您的位置:首页 > 编程语言 > Java开发

Spring+Mybatis 整合例子

2017-08-20 08:58 351 查看
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd"> <modelVersion>4.0.0</modelVersion>
<groupId>cn.hello</groupId>
<artifactId>Sping-ba</artifactId>
<packaging>war</packaging>
<version>1.0-SNAPSHOT</version>
<name>Sping-ba Maven Webapp</name>
<url>http://maven.apache.org</url>
<dependencies>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.12</version>
<scope>test</scope>
</dependency>

<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-beans</artifactId>
<version>4.2.3.RELEASE</version>
</dependency>

<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context</artifactId>
<version>4.2.2.RELEASE</version>>
</dependency>

<!-- <!–aop–>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-aop</artifactId>
<version>4.3.4.RELEASE</version>
</dependency>-->

<!--Aop 依赖包-->
<dependency>
<groupId>org.apache.geronimo.bundles</groupId>
<artifactId>aspectjweaver</artifactId>
<version>1.6.8_2</version>
</dependency>

<!--Aop 依赖包-->
<!-- <dependency>
<groupId> org.aspectj</groupId >
<artifactId> aspectjweaver</artifactId >
<version> 1.8.7</version>>
</dependency>-->

<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-jdbc</artifactId>
<version>4.3.3.RELEASE</version>
</dependency>

<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>5.1.31</version>
</dependency>

<!--c3p0-->
<dependency>
<groupId>com.mchange</groupId>
<artifactId>c3p0</artifactId>
<version>0.9.5.2</version>
</dependency>

<!-- dbcp -->
<dependency>
<groupId>commons-dbcp</groupId>
<artifactId>commons-dbcp</artifactId>
<version>1.2.2</version>
</dependency>

<!-- alibaba -->
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>druid</artifactId>
<version>1.0.26</version>
</dependency>

<dependency>
<groupId>org.mybatis</groupId>
<artifactId>mybatis</artifactId>
<version>3.2.2</version>
</dependency>

<!--mybatis+Spring-->
<dependency>
<groupId>org.mybatis</groupId>
<artifactId>mybatis-spring</artifactId>
<version>1.3.1</version>
</dependency>

<dependency>
<groupId>javax</groupId>
<artifactId>javaee-api</artifactId>
<version>7.0</version>
<scope>provided</scope>
</dependency>

<dependency>
<groupId>javax.servlet</groupId>
<artifactId>jstl</artifactId>
<version>1.2</version>
</dependency>

<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-web</artifactId>
<version>4.3.9.RELEASE</version>>
</dependency>

</dependencies>
<!-- <build>
<finalName>Sping-ba</finalName>
</build>-->
<build>
<resources>
<resource>
<directory>src/main/java</directory>
<includes>
<include>**/*.xml</include>
</includes>
</resource>
</resources>
</build>

</project>
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE configuration
PUBLIC "-//mybatis.org//DTD Config 3.0//EN"
"http://mybatis.org/dtd/mybatis-3-config.dtd">
<configuration><!--根节点-->
<!-- <properties resource="database.properties"></properties>-->
<!--别名的设定-->
<typeAliases>
<package name="cn.springJDBC_SSM.cn.cn.entity"></package>
</typeAliases>
<mappers>
<package name="cn.springJDBC_SSM.cn.cn.dao"></package>
</mappers>
</configuration>

driver=com.mysql.jdbc.Driver
url=jdbc:mysql:///bookshop
user=root
password=a123456a
<?xml version="1.0" encoding="UTF-8"?>
<beans  xmlns="http://www.springframework.org/schema/beans"
xmlns:aop="http://www.springframework.org/schema/aop"
xmlns:tx="http://www.springframework.org/schema/tx"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:p="http://www.springframework.org/schema/p"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation=" http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd
">
<!--00.识别jdbc.properties文件-->
<context:property-placeholder location="classpath:jdbc.properties"></context:property-placeholder>

<!--alibaba 数据源文件-->
<bean id="dataSource" class="com.alibaba.druid.pool.DruidDataSource">
<property name="driverClassName" value="${driver}"></property>
<property name="url" value="${url}"></property>
<property name="username" value="${user}"></property>
<property name="password" value="${password}"></property>
</bean>

<!--工厂配置-->
<bean id="sessinoFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
<property name="dataSource" ref="dataSource"></p
ae05
roperty>
<property name="configLocation" value="classpath:mybatis-config.xml"></property>
</bean>

<!--cn.cn.dao  实现类-->
<bean id="bookDAO" class="org.mybatis.spring.mapper.MapperScannerConfigurer">
<property name="basePackage" value="cn.springJDBC_SSM.cn.cn.dao"></property>
<property name="sqlSessionFactoryBeanName" value="sessinoFactory"></property>
</bean>

<!--cn.cn.service-->
<bean id="bookService" class="cn.springJDBC_SSM.cn.cn.service.BookServiceImpl">
<property name="cn.cn.dao" ref="IBookDAO"></property>
</bean>

<!--<!–事务管理器–>
<bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
<property name="dataSource" ref="dataSource"></property>
</bean>

<!–第三种 –>
<tx:advice id="txAdvice" transaction-manager="transactionManager">
<tx:attributes>
<tx:method name="buy*" isolation="DEFAULT" propagation="REQUIRED" rollback-for="StockException"/>
</tx:attributes>
</tx:advice>
<aop:config>
<!–配置切点–>
<aop:pointcut id="mypoint" expression="execution(* *..cn.cn.service.*.*(..))"></aop:pointcut>
<!–顾问–>
<aop:advisor advice-ref="txAdvice" pointcut-ref="mypoint"></aop:advisor>
</aop:config>-->
</beans>


public class Book {

private  Integer bookId;
private  String bookName;
private  Integer bookPrice;

public Integer getBookId() {
return bookId;
}

public void setBookId(Integer bookId) {
this.bookId = bookId;
}

public String getBookName() {
return bookName;
}

public void setBookName(String bookName) {
this.bookName = bookName;
}

public Integer getBookPrice() {
return bookPrice;
}

public void setBookPrice(Integer bookPrice) {
this.bookPrice = bookPrice;
}
}
public interface IBookDAO {
public  int addBook(Book book);
}

<?xml version="1.0" encoding="UTF-8"?>

<!DOCTYPE mapper
PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
"http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<mapper namespace="cn.springJDBC_SSM.cn.cn.dao.IBookDAO">
<insert id="addBook">
INSERT  INTO book(bookName,bookPrice) VALUES (#{bookName},#{bookPrice})
</insert>

</mapper>

<!DOCTYPE web-app PUBLIC
"-//Sun Microsystems, Inc.//DTD Web Application 2.3//EN"
"http://java.sun.com/dtd/web-app_2_3.dtd" >

<web-app>
<display-name>Archetype Created Web Application</display-name>

<context-param>
<param-name>contextConfigLocation</param-name>

<param-value>classpath:applicationContext12SSM.xml</param-value>
</context-param>
<!--配置监听器-->
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>

</web-app>

@WebServlet(name = "BookServlet",urlPatterns = "/BookServlet")
public class BookServlet extends HttpServlet {
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {

request.setCharacterEncoding("UTF-8");
String bookName=request.getParameter("bookName");
int bookPrice=Integer.parseInt(request.getParameter("bookPrice"));

Book book=new Book();
book.setBookName(bookName);
book.setBookPrice(bookPrice);
// ApplicationContext ctx=new ClassPathXmlApplicationContext("applicationContext12SSM.xml");
WebApplicationContext ctx = WebApplicationContextUtils.getRequiredWebApplicationContext(getServletContext());
System.out.println(ctx+"===");
BookService service = (BookService)ctx.getBean("bookService");
int conut = service.addBook(book);
if (conut>0){
request.getRequestDispatcher("/index.jsp").forward(request,response);
}else {
request.getRequestDispatcher("/1.jsp").forward(request,response);
}

}

protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
doPost(request,response);
}
}

<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>

</head>
<body>

<form action="/BookServlet" method="post">
<input name="bookName"/>
<input name="bookPrice"/>
<input type="submit" value="添加"/>
</form>

</body>
</html>
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: