Mybatis----整合Spring ---配置
1,spring整合mybatis的意思是原有的mybatis中的配置内容,合并到spring中。:如,连接数据库等。
2,spring整合mybatis的步骤
(1) 原来mybatis中提供sqlsession对象的一个工具:sqlsessionFactory
在spring 中 sqlsessionFactoryBean
在spring配置文件里头配置一个
<!--配置工厂bean-->
<bean id="sesiionFactroy" class="org.mybatis.spring.SqlSessionFactoryBean">
<property name="dataSource" ref="dataSource"></property>
<property name="configLocation" value="classpath:mybatis-config.xml"></property>
</bean>
(2)映射文件mapper.xml
dao 层的配置,因为dao层没有实现类,但是我们有dao层的xml文件。使用
MapperScannerConfigurer类 去扫描 dao' 层xml文件。
在sprng中配置如下:
<bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
<property name="basePackage" value="cn.happy.spring16ssm.dao"></property>
<property name="sqlSessionFactoryBeanName" value="sesiionFactroy"></property>
</bean>
3,案列:添加图书的功能(来了解spring整合mybatis的步骤)
(1)数据库
(2)实体层
package cn.happy.spring16ssm.entity; 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; } }
(3)dao层
IBookDAO接口
package cn.happy.spring16ssm.dao; import cn.happy.spring16ssm.entity.Book; public interface IBookDAO { public int addbook(Book book); }
IBookDAO.xml
<?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"> <!--映射文件的根节点 namespace --> <mapper namespace="cn.happy.spring16ssm.dao.IBookDAO"> <insert id="addbook"> INSERT INTO book(bookname,bookprice) VALUES (#{bookname},#{bookprice}) </insert> </mapper>
(4)service层
IBookService
package cn.happy.spring16ssm.service; import cn.happy.spring16ssm.entity.Book; public interface IBookService { public int addbook(Book book); }
IBookServiceImpl
import cn.happy.spring16ssm.entity.Book; import cn.happy.spring16ssm.service.IBookService; public class IBookServiceImpl implements IBookService { //植入dao层 IBookDAO dao; public int addbook(Book book) { return dao.addbook(book); } public IBookDAO getDao() { return dao; } public void setDao(IBookDAO dao) { this.dao = dao; } }
(5)spring配置文件applicationContest.xml文件
<?xml version="1.0" encoding="UTF-8"?> <beans xmlns="http://www.springframework.org/schema/beans" xmlns:tx="http://www.springframework.org/schema/tx" xmlns:aop="http://www.springframework.org/schema/aop" xmlns:context="http://www.springframework.org/schema/context" 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-2.5.xsd http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx.xsd "> <!--使用propertyPlaceholder--> <bean class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer"> <property name="location" value="classpath:jdbc.properties"/> </bean> <!--bncp的驱动--> <bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource"> <property name="url" value="${jdbc.url}"></property> <property name="driverClassName" value="${jdbc.driverClassName}"></property> <property name="username" value="${jdbc.username}"></property> <property name="password" value="${jdbc.password}"></property> </bean> <!--配置工厂bean--> <bean id="sesiionFactroy" class="org.mybatis.spring.SqlSessionFactoryBean"> <property name="dataSource" ref="dataSource"></property> <property name="configLocation" value="classpath:mybatis-config.xml"></property> </bean> <!--dao--> <bean class="org.mybatis.spring.mapper.MapperScannerConfigurer"> <property name="basePackage" value="cn.happy.spring16ssm.dao"></property> <property name="sqlSessionFactoryBeanName" value="sesiionFactroy"></property> </bean> <!--service--> <bean id="bookService" class="cn.happy.spring16ssm.service.impl.IBookServiceImpl"> <property name="dao" ref="IBookDAO"></property> </bean> <!--平台事务管理器--> <bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager"> <!--注入什么东西 事务对象的来源,一定是Connection --> <property name="dataSource" ref="dataSource"></property> </bean> <!--事务方式2:使用AspectJaop 实现事务--> <tx:advice id="txAdvice" transaction-manager="transactionManager"> <tx:attributes> <tx:method name="addbook" isolation="DEFAULT" propagation="REQUIRED"/> </tx:attributes> </tx:advice> <aop:config> <!--设置切点point--> <aop:pointcut id="mypoint" expression="execution(* *..spring16ssm.service.*.*(..))"/> <!--设置顾问--> <aop:advisor advice-ref="txAdvice" pointcut-ref="mypoint"></aop:advisor> </aop:config> </beans>
(6)mybatis配置文件 mybatis-cinfig.xml
<?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="jdbc1.properties"></properties>--> <typeAliases> <package name="cn.happy.spring16ssm.entity"></package> </typeAliases> <!--下面的papper配置节点可以省略--> <mappers> <package name="cn.happy.spring16ssm.dao"></package> </mappers> </configuration>
(7)jdbc.properties文件
jdbc.driverClassName=com.mysql.jdbc.Driver jdbc.url=jdbc:mysql:///mynews?characterEncoding=UTF-8 jdbc.username=root jdbc.password=
(8)pom.xml 依赖文件
<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"> <parent> <artifactId>MyBatis01</artifactId> <groupId>cn.happy</groupId> <version>1.0-SNAPSHOT</version> </parent> <modelVersion>4.0.0</modelVersion> <artifactId>spring01</artifactId> <packaging>war</packaging> <name>spring01 Maven Webapp</name> <url>http://maven.apache.org</url> <dependencies> <dependency> <groupId>junit</groupId> <artifactId>junit</artifactId> <version>3.8.1</version> <scope>test</scope> </dependency> <!--引入beans节点--> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-beans</artifactId> <version>4.2.3.RELEASE</version> </dependency> <!--context--> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-context</artifactId> <version>4.2.2.RELEASE</version> </dependency> <!--后加的依赖--> <dependency> <groupId> org.aspectj</groupId > <artifactId> aspectjweaver</artifactId > <version> 1.8.7</version > </dependency> <!--spring JDBC--> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-jdbc</artifactId> <version>4.2.0.RELEASE</version> </dependency> <dependency> <groupId>org.wisdom-framework</groupId> <artifactId>mysql-connector-java</artifactId> <version>5.1.34_1</version> </dependency> <!--dbcp 数据源--> <!-- https://mvnrepository.com/artifact/commons-dbcp/commons-dbcp --> <dependency> <groupId>commons-dbcp</groupId> <artifactId>commons-dbcp</artifactId> <version>1.4</version> </dependency> <!--c3p0--> <!-- https://mvnrepository.com/artifact/c3p0/c3p0 --> <!-- https://mvnrepository.com/artifact/c3p0/c3p0 --> <dependency> <groupId>com.mchange</groupId> <artifactId>c3p0</artifactId> <version>0.9.5.2</version> </dependency> <!--alibaba datasource--> <!-- https://mvnrepository.com/artifact/com.alibaba/druid --> <dependency> <groupId>com.alibaba</groupId> <artifactId>druid</artifactId> <version>1.0.18</version> </dependency> <!--Mybatis+Spring整合--> <dependency> <groupId>org.mybatis</groupId> <artifactId>mybatis-spring</artifactId> <version>1.2.0</version> </dependency> <!--mybatis jar包--> <dependency> <groupId>org.mybatis</groupId> <artifactId>mybatis</artifactId> <version>3.2.2</version> </dependency> <!--javaee的jar包--> <dependency> <groupId>javaee</groupId> <artifactId>javaee-api</artifactId> <version>5</version> </dependency> <!--jsp进行循环迭代数据,使用jstl--> <dependency> <groupId>javax.servlet</groupId> <artifactId>jstl</artifactId> <version>1.2</version> <scope>runtime</scope> </dependency> <!-- Spring整合JavaWeb的包 --> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-web</artifactId> <version>4.3.9.RELEASE</version> </dependency> <dependency> <groupId>org.junit.jupiter</groupId> <artifactId>junit-jupiter-api</artifactId> <version>RELEASE</version> </dependency> <dependency> <groupId>junit</groupId> <artifactId>junit</artifactId> <version>RELEASE</version> </dependency> <dependency> <groupId>com.alibaba</groupId> <artifactId>druid</artifactId> <version>1.0.18</version> </dependency> </dependencies> <build> <resources> <resource> <directory>src/main/java</directory> <includes> <include>**/*.*</include> </includes> </resource> </resources> </build> </project>
(9)不要忘了pom.xml文件里头的 build 节点的配置和 mybatis-spring 的依赖
<build>
<resources>
<resource>
<directory>src/main/java</directory>
<includes>
<include>**/*.*</include>
</includes>
</resource>
</resources>
</build>
依赖:
<!--Mybatis+Spring整合-->
<dependency>
<groupId>org.mybatis</groupId>
<artifactId>mybatis-spring</artifactId>
<version>1.2.0</version>
</dependency>
(10)测试
package cn.happy.SpringMybatis; import cn.happy.spring16ssm.entity.Book; import cn.happy.spring16ssm.service.IBookService; import org.springframework.context.ApplicationContext; import org.springframework.context.support.ClassPathXmlApplicationContext; public class Test { // Spring 事务测试 @org.junit.Test public void test04() { ApplicationContext ctx = new ClassPathXmlApplicationContext("applicationContextSpring15mybatis.xml"); IBookService service = (IBookService) ctx.getBean("bookService"); Book book=new Book(); book.setBookname("狼图腾"); book.setBookprice(78); service.addbook(book); } }
(11)测试结果