Sping和MyBatis的整合
生活随笔
收集整理的這篇文章主要介紹了
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
總結
以上是生活随笔為你收集整理的Sping和MyBatis的整合的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: GAMS系列分享3-GAMS基础知识-集
- 下一篇: word文档怎么找回误删的文件_Word