spring整合Mybatis
思想: 整合一定不是两个框架jar包的简单的堆砌
真正的思想是Spring接管Mybatis框架的一部分工作
现在通过案例来演示
数据库
按照数据库建立实体类
public class book { private Integer bid; private String bname; private Integer bprice; public Integer getBid() { return bid; } public void setBid(Integer bid) { this.bid = bid; } public String getBname() { return bname; } public void setBname(String bname) { this.bname = bname; } public Integer getBprice() { return bprice; } public void setBprice(Integer bprice) { this.bprice = bprice; } }
现在开始就需要 mybatis的大小配置 小配置和接口连接一下
public interface IBook { public int insertbook(book bo); }
<?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="SprMy.dao.IBook"> <insert id="insertbook"> insert into books(bname,bprice) VALUES(#{bname},#{bprice}) </insert> </mapper>
<?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> </configuration>
以及service层
public interface BookService { public int insertbook(book bo); }
public class BookServiceimpl implements BookService { private IBook bk; public IBook getBk() { return bk; } public void setBk(IBook bk) { this.bk = bk; } @Transactional(isolation = Isolation.DEFAULT,propagation = Propagation.REQUIRED) public int insertbook(book bo) { return bk.insertbook(bo); } }
实现类里,有一个接口的对象,getset一下 直接调用方法就行
现在开始spring配置
<bean id="datasource" class="org.springframework.jdbc.datasource.DriverManagerDataSource">
<property name="driverClassName" value="com.mysql.jdbc.Driver"></property>
<property name="url" value="jdbc:mysql:///newss2228"></property>
<property name="username" value="root"></property>
<property name="password" value=""></property>
</bean>
数据源
<bean id="transation" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
<property name="dataSource" ref="datasource"></property>
</bean>
<tx:annotation-driven transaction-manager="transation"></tx:annotation-driven>
事务管理
<bean class="org.mybatis.spring.SqlSessionFactoryBean"> <property name="dataSource" ref="datasource"></property> <property name="configLocation" value="classpath:mybatis-config.xml"></property> </bean> <bean class="org.mybatis.spring.mapper.MapperScannerConfigurer"> <property name="basePackage" value="SprMy.dao"></property> </bean> <bean id="service" class="SprMy.service.impl.BookServiceimpl"> <property name="bk" ref="IBook"></property> </bean>
<property name="configLocation" value="classpath:mybatis-config.xml"></property> 这是大配置文件名
<property name="basePackage" value="SprMy.dao"></property> 这是接口的包
<property name="bk" ref="IBook"></property> 连接上面的接口,由于接口不能不能创建bean,所以,
这里就写接口的名字就行,报出错误不用管 ,有命名规则,I开头用原来的名,不是的话用小写的名。
在webxml中加入以下代码,提高一下性能
<context-param> <param-name>contextConfigLocation</param-name> <param-value>classpath:application-06.xml</param-value> </context-param> <listener> <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class> </listener>
<param-value>classpath:application-06.xml</param-value> 这是spring的配置名
现在看使用
在jsp中使用
request.setCharacterEncoding("UTF-8"); String name=request.getParameter("bookname"); String price=request.getParameter("bookprice"); Integer pr=Integer.parseInt(price); String author=request.getParameter("bookauthor"); book book=new book(); book.setBname(name); book.setBprice(pr); ApplicationContext context= WebApplicationContextUtils.getRequiredWebApplicationContext(getServletContext()); BookService service = (BookService) context.getBean("service"); int i = service.insertbook(book); if (i>0){ request.getRequestDispatcher("/insert.jsp").forward(request,response); }else{ response.sendRedirect("/insert.jsp"); }
现在给一下页面
<%@ page contentType="text/html;charset=UTF-8" language="java" %> <html> <head> <title>Title</title> </head> <body> <h1>添加图书</h1> <form action="/BooknewServlet" method="post"> 图书名称:<input name="bookname"/> 图书作者:<input name="bookauthor"/> 图书价格:<input name="bookprice"/> <input type="submit" value="添加"/> </form> </body> </html>
这就是整合的一个案例了,通过这个学习,我们就能认识整合