Spring+MyBatis整合、接口实现组件两种方法、整合流程
来源:互联网 发布:淘宝多个订单虚假交易 编辑:程序博客网 时间:2024/05/08 13:56
Spring+Mybatis
mybatis-spring.jar整合包
MapperFactoryBean组件,封装了根据Mapper映射器接口生成实现组件的功能
结合
先把包都引入 ioc aop dao dbcp 当然在之前的Mybatis下增加的
当然还有配置文件
还有引入 mybatis-spring.jar
在Spring配置中
<!-- 可以根据给定的Mapper接口生成实现组件 --> <bean id="costDao" class="org.mybatis.spring.mapper.MapperFactoryBean"> <property name="mapperInterface" value="dao.CostDao"></property> </bean>
实现一个组件就要写一个这个bean
那个dao.CostDao接口就是之前写的接口。让他根据这个接口实现组件
SqlSessionFactoryBean组件,封装了创建SqlSession
用dbcp的连接池
就是不用MyBatis的配置文件了。用的dbcp的连接池
即:用这个dbcp连接池的bean
<!-- 定义dbcp的DataSource --> <bean id="dbcp" class="org.apache.commons.dbcp.BasicDataSource"> <property name="username" value="root"></property> <property name="password" value="root"></property> <property name="url" value="jdbc:mysql:///jsd1507db"></property> <property name="driverClassName" value="com.mysql.jdbc.Driver"></property> </bean> 替代:mybatis的配置文件 <environments default="development"> <environment id="development"> <transactionManager type="JDBC" /> <dataSource type="POOLED"> <property name="driver" value="com.mysql.jdbc.Driver" /> <property name="url" value="jdbc:mysql://localhost/jsd1507db" /> <property name="username" value="root" /> <property name="password" value="root" /> </dataSource> </environment> </environments>
这几个关系
DataSource(dbcp)
SqlSessionFactoryBean(ssf)
注入dataSource
注入sql定义文件
MapperFactoryBean(costDAO)
注入sqlsessionfactory
注入CostDao接口
<!-- 可以根据给定的Mapper接口生成实现组件 --> <bean id="costDao" class="org.mybatis.spring.mapper.MapperFactoryBean"> <property name="mapperInterface" value="dao.CostDao"></property> <!-- 指定sqlsession资源 --> <property name="sqlSessionFactory" ref="ssf"></property> </bean> <bean id="ssf" class="org.mybatis.spring.SqlSessionFactoryBean"> <!-- 注入dataSource 用连接池获取--> <property name="dataSource" ref="dbcp"></property> <!-- 注入SQL语句文件 用*号代替,那么就可以加载多个xml文件了 --> <property name="mapperLocations" value="classpatch:entity/*.xml"></property> </bean> <!-- 定义dbcp的DataSource --> <bean id="dbcp" class="org.apache.commons.dbcp.BasicDataSource"> <property name="username" value="root"></property> <property name="password" value="root"></property> <property name="url" value="jdbc:mysql:///jsd1507db"></property> <property name="driverClassName" value="com.mysql.jdbc.Driver"></property> </bean>
一定要注意!!!!!!!!!!!!
编写的Mapper接口的名字要和Mapper.xml文件中的namespace相同才可以
<mapper namespace="dao.CostDao">
要把包也写上。
利用MapperFactoryBean
主要是配置文件:
<!-- 可以根据给定的Mapper接口生成实现组件 --> <bean id="costDao" class="org.mybatis.spring.mapper.MapperFactoryBean"> <property name="mapperInterface" value="dao.CostDao"></property> <!-- 指定sqlsession资源 --> <property name="sqlSessionFactory" ref="ssf"></property> </bean> <bean id="ssf" class="org.mybatis.spring.SqlSessionFactoryBean"> <!-- 注入dataSource 用连接池获取--> <property name="dataSource" ref="dbcp"></property> <!-- 注入SQL语句文件 用*号代替,那么就可以加载多个xml文件了 --> <property name="mapperLocations" value="entity/CostDao.xml"></property> </bean> <!-- 定义dbcp的DataSource --> <bean id="dbcp" class="org.apache.commons.dbcp.BasicDataSource"> <property name="username" value="root"></property> <property name="password" value="root"></property> <property name="url" value="jdbc:mysql:///jsd1507db"></property> <property name="driverClassName" value="com.mysql.jdbc.Driver"></property> </bean>
测试类:package test;import java.util.List;import org.springframework.context.ApplicationContext;import org.springframework.context.support.ClassPathXmlApplicationContext;import dao.CostDao;import entity.Cost;public class TestCostDao { public static void main(String[] args){ String conf ="applicationContext.xml"; ApplicationContext ac = new ClassPathXmlApplicationContext(conf); CostDao costDao = ac.getBean("costDao",CostDao.class); List<Cost> list =costDao.findAll(); for(Cost cost:list){ System.out.println("name:"+cost.getName()); } }}
sql语句文件Mapper<?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="dao.CostDao"> <select id="findAll" resultType="entity.Cost"> select * from cost </select> <select id="findPage" parameterType="int" resultType="entity.Cost"> select * from cost limit #{page},5 </select> <select id="findMap" resultType="Map"> select cost_id,name from cost </select> </mapper>
MapperScannerConfigurer
这个组件封装了批量生成Mapper组件的功能
批量为每一个dao生成
如下配置
<!-- 可以根据指定路径批量生成Dao实现 --> <bean class="org.mybatis.spring.mapper.MapperScannerConfigurer"> <!-- 扫描dao下所有接口,批量生成实现 --> <property name="basePackage" value="dao"></property> <!-- 自动注入sqlsessionfactory --> <property name="sqlSessionFactory" ref="ssf"></property> </bean> <bean id="ssf" class="org.mybatis.spring.SqlSessionFactoryBean"> <!-- 注入dataSource 用连接池获取--> <property name="dataSource" ref="dbcp"></property> <!-- 注入SQL语句文件 用*号代替,那么就可以加载多个xml文件了 --> <property name="mapperLocations" value="entity/CostDao.xml"></property> </bean> <!-- 定义dbcp的DataSource --> <bean id="dbcp" class="org.apache.commons.dbcp.BasicDataSource"> <property name="username" value="root"></property> <property name="password" value="root"></property> <property name="url" value="jdbc:mysql:///jsd1507db"></property> <property name="driverClassName" value="com.mysql.jdbc.Driver"></property> </bean>也会生成一个costDAO组件直接就可以用。测试类就是原来的测试类 <!-- 若不是想让全部的dao里的都实现的话就如下。有定义的注解的才会实现 .没有就不会实现--> <property name="annotationClass" value="dao.annotation.mybatisdao"></property>
即创建个接口的话如果有@mybatisdao 。这个是自己的写的。就是上面value的值的最后。
Dao接口实现可以是框架字段给生成。(就是利用如上的两种方式)
也可以是自己编写利用 SqlSessionTemplate组件
整合流程、总结
先导入包:
导入配置文件
编写对应数据库表的实体类 entity.User
package entity;import java.io.Serializable;public class User implements Serializable{ private Integer id; private String username; private String pwd; private String name; private String gender; public Integer getId() { return id; } public void setId(Integer id) { this.id = id; } public String getUsername() { return username; } public void setUsername(String username) { this.username = username; } public String getPwd() { return pwd; } public void setPwd(String pwd) { this.pwd = pwd; } public String getName() { return name; } public void setName(String name) { this.name = name; } public String getGender() { return gender; } public void setGender(String gender) { this.gender = gender; }}
然后编写SQL文件
<?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="dao.UserDao"> <select id="findByName" parameterType="String" resultType="entity.User"> select * from user where username=#{username} </select> </mapper>
编写接口Mapper
package dao;import entity.User;public interface UserDao { public User findByName(String name);}
!!!进行配置:
<?xml version="1.0" encoding="UTF-8"?><beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:context="http://www.springframework.org/schema/context" xmlns:util="http://www.springframework.org/schema/util" xmlns:jee="http://www.springframework.org/schema/jee" xmlns:tx="http://www.springframework.org/schema/tx" xmlns:jpa="http://www.springframework.org/schema/data/jpa" xmlns:mvc="http://www.springframework.org/schema/mvc" xsi:schemaLocation=" http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.2.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.2.xsd http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util-3.2.xsd http://www.springframework.org/schema/jee http://www.springframework.org/schema/jee/spring-jee-3.2.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.2.xsd http://www.springframework.org/schema/data/jpa http://www.springframework.org/schema/data/jpa/spring-jpa-1.3.xsd http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-3.2.xsd"> <!-- 定义dbcp --> <bean id="dbcp" class="org.apache.commons.dbcp.BasicDataSource"> <property name="username" value="root"></property> <property name="password" value="root"></property> <property name="url" value="jdbc:mysql:///jsd1507db"></property> <property name="driverClassName" value="com.mysql.jdbc.Driver"></property> </bean> <bean id="ssf" class="org.mybatis.spring.SqlSessionFactoryBean"> <!-- 注入dataSource 用连接池获取--> <property name="dataSource" ref="dbcp"></property> <!-- 注入SQL语句文件 用*号代替,那么就可以加载多个xml文件了 --> <property name="mapperLocations" value="sql/UserDao.xml"></property> </bean> <bean class="org.mybatis.spring.mapper.MapperScannerConfigurer"> <!-- 扫描dao下所有接口,批量生成实现 --> <property name="basePackage" value="dao"></property> <!-- 自动注入sqlsessionfactory --> <property name="sqlSessionFactory" ref="ssf"></property> </bean></beans>
测试类
package test;import org.springframework.context.ApplicationContext;import org.springframework.context.support.ClassPathXmlApplicationContext;import dao.UserDao;import entity.User;public class TestUserDao { public static void main(String[] args){ String conf ="applicationContext.xml"; ApplicationContext ac = new ClassPathXmlApplicationContext(conf); UserDao userDao = ac.getBean("userDao",UserDao.class); User user = userDao.findByName("Recar"); System.out.println("pwd: "+user.getPwd()); }}
一定要注意细节配置。还有 <mapper namespace="dao.UserDao">
的namespace一定要是包加接口名!!!
- Spring+MyBatis整合、接口实现组件两种方法、整合流程
- spring + mybatis 框架整合的两种方法
- spring整合mybatis方法总结(两种)
- spring与mybatis三种整合方法
- spring与mybatis三种整合方法
- spring与mybatis三种整合方法
- spring与mybatis三种整合方法
- spring与mybatis三种整合方法
- spring与mybatis三种整合方法
- spring与mybatis三种整合方法
- spring与mybatis三种整合方法
- spring与mybatis三种整合方法
- spring与mybatis三种整合方法
- spring与mybatis三种整合方法
- spring与mybatis三种整合方法
- spring与mybatis三种整合方法
- spring与mybatis三种整合方法
- spring与mybatis三种整合方法
- ORA-12838: 无法在并行模式下修改之后读/修改对象
- Huffman编码---C++
- Vue2.0中v-for迭代语法变化(key、index)
- MongoDB安装说明
- spring4 + quartz2初探
- Spring+MyBatis整合、接口实现组件两种方法、整合流程
- 锁、事务、隔离级别--readonly
- JS删除数组里的某个元素
- 25 Python 包
- 形式语言与自动机学习心得
- MySQL索引类型normal,unique,full text的区别是什么?
- 严重: Servlet.service() for servlet jsp threw exception org.apache.jasper.JasperException: /WEB-INF/js
- 系统和压缩内存 NT Kernel 80端口被pid=4的进程占用,清除NT Kernel占用
- Android内存溢出与优化(零)——开题篇