Sping和MyBatis的整合

       作为Bean容器,Sping框架提供了Ioc机制,可以接管所有组件的创建工作进行依赖管理,因而整合的主要工作就是把MyBatis框架使用中涉及的核心组件配置到Sping容器中,交给Sping来创建和管理。

       业务逻辑对象依赖于MyBatis技术实现的Dao对象,核心是获取SqlSession实例。而SqlSession是通过SqlSessionFactoryBen来获取。

    Sping 和MyBatis整合方法:

    在整合是用到MyBatis-Sping-1.2.0jar

      

<!--Mybatis+Spring整合-->
<dependency>
<groupId>org.mybatis</groupId>
<artifactId>mybatis-spring</artifactId>
<version>1.2.0</version>
</dependency>

1、采用MapperScannerConfigurer,它将会查找类路径下的映射器并自动将它们创建成MapperFactoryBean。
spring-mybatis.xml:

   例:图书添加。

       建立数据库:

          

       创建book实体:

        

private int id;
private String bookname;
private int bookprice;

public int getId() {
return id;
}

public void setId(int id) {
this.id = id;
}

public String getBookname() {
return bookname;
}

public void setBookname(String bookname) {
this.bookname = bookname;
}

public int getBookprice() {
return bookprice;
}

public void setBookprice(int bookprice) {
this.bookprice = bookprice;
}


创建IBookDAO

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.day16ssm.dao.IBookDAO">
<insert id="addbook">
insert into bookshop(bookname,bookprice) values(#{bookname},#{bookprice})
</insert>
</mapper>
service层:
IBookService:
public interface IBookService {
public int addbook(book book);
}
BookServiceimpl:
public class BookServiceimpl implements IBookService {
private IBookDAO dao;

public IBookDAO getDao() {
return dao;
}

public void setDao(IBookDAO dao) {
this.dao = dao;
}

public int addbook(book book) {
return dao.addbook(book);

}
}
JDBC的四大属性:
jdbc.url=jdbc:mysql:///bookshop
jdbc.driverClassName=com.mysql.jdbc.Driver
jdbc.username=root
jdbc.password=
MyBatis的配置文件:
<?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>
<!--别名的设置-->
<typeAliases>
<package name="cn.happy.day16ssm.entity"></package>
</typeAliases>
<!--管理小配置-->
</configuration>
配置文件:
<?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
">
<bean class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
<property value="classpath:jdbc.properties" name="location"></property>
</bean>

<!--阿里数据源-->
<!--阿里数据源 不想进入阿里的程序员,不是好厨师-->
<bean id="dataSource" class="com.alibaba.druid.pool.DruidDataSource">
<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>
<!--将Mybatis SqlsessionFactory-->
<bean class="org.mybatis.spring.SqlSessionFactoryBean">
<property name="dataSource" ref="dataSource"></property>
<property name="configLocation" value="classpath:mybatis-config.xml"></property>
</bean>

<!--bookDAO 根据工厂生成session-->
<bean class="org.mybatis.spring.mapper.MapperScannerConfigurer" >
<property name="basePackage" value="cn.happy.day16ssm.dao"></property>
</bean>
<!--4.service-->
<bean id="bookService" class="cn.happy.day16ssm.service.BookServiceimpl">
<property name="dao" ref="IBookDAO"></property>

</bean>

<!--平台事务管理器-->
<bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
<!--注入什么东西 事务对象的来源,一定是Connection -->
<property name="dataSource" ref="dataSource"></property>
</bean>

<!--方式三:Aspectj AOP 方式管理事物-->
<tx:advice id="txadvice" transaction-manager="transactionManager">
<tx:attributes>
<tx:method name="addbook" isolation="DEFAULT" propagation="REQUIRED" />
</tx:attributes>
</tx:advice>
<aop:config>
<aop:pointcut id="mypoint" expression="execution(* *..day16ssm.service.*.*(..))"></aop:pointcut>
<aop:advisor advice-ref="txadvice" pointcut-ref="mypoint"></aop:advisor>
</aop:config>
</beans>

测试类:
public class test16ssm {
@Test
public void t01(){
ApplicationContext ac=new ClassPathXmlApplicationContext("applicationContextStockday16ssm.xml");
IBookService service=(IBookService)ac.getBean("bookService");
book book=new book();
book.setBookname("三国演义");
book.setBookprice(200);
service.addbook(book);

}
}
运行结果:

       运行结果:

         

原文地址:https://www.cnblogs.com/lsj0404/p/7704725.html