MyBatis集成Spring开发 讲授
简介:Spring集成Mybatis开发简述有两种方式,第1种是在applicationContext.xml中配置接口扫描类(同时也扫描了sql.xml配置文件)或注入接口类(MapperScannerConfigurer、MapperFactoryBean这两个在test中有讲授如何配置),第2种是原生的Mybatis,不用接口开发,而在applicationContext.xml中当配置sqlSessionFactory时候,配置如conf.xml文件,让Mybatis自己扫描,从而在程序中使用原生Mybatis做CRUD操作。
注意点,在上1篇文章中写到了Mybatis的注解方式也是接口,在spring中的接口和注解方式是不1样的,注解方式的时候定义了接口,要在接口中写入配置,比如@Select("select * from Users where
id = #{id}")等,不需要对应的sql.xml配置文件,2在Spring中,写入了接口,还需要对应的sql.xml配置文件,而这个文件的namespace就是对应的接口全名,使用MapperScannerConfigurer扫描了接口类型后,在调用的时候使用接口类型.save()等方法来实现CRUD。
1、项目清单
2、顺序源码
package com.bjsxt.bean;
public class User {
//shift+alt+s
private int id;
private String name;
private int age;
public User(int id, String name, int age) {
super();
this.id = id;
this.name = name;
this.age = age;
}
public User() {
super();
}
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
@Override
public String toString() {
return "User [id=" + id + ", name=" + name + ", age=" + age + "]";
}
}
package com.bjsxt.user.dao;
import java.util.List;
import com.bjsxt.bean.User;
public interface UserMapper {
void save(User user);
void update(User user);
void delete(int id);
User findById(int id);
List<User> findAll();
}
package com.bjsxt.user.dao;
import java.util.List;
import com.bjsxt.bean.User;
public interface UserMapper2 {
void save(User user);
void update(User user);
void delete(int id);
User findById(int id);
List<User> findAll();
}
<?xml version="1.0" encoding="UTF⑻"?>
<!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
"http://mybatis.org/dtd/mybatis⑶-mapper.dtd">
<!-- spring集成Mybatis namespace 必须是接口的全类名 -->
<mapper namespace="com.bjsxt.user.dao.UserMapper">
<insert id="save" parameterType="User" >
insert into users(name,age) values(#{name},#{age})
</insert>
</mapper>
<?xml version="1.0" encoding="UTF⑻"?>
<!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
"http://mybatis.org/dtd/mybatis⑶-mapper.dtd">
<!-- spring集成Mybatis namespace 必须是接口的全类名 -->
<mapper namespace="com.bjsxt.user.dao.UserMapper2">
<insert id="save" parameterType="User" >
insert into users(name,age) values(#{name},#{age})
</insert>
</mapper>
package com.test;
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import org.springframework.stereotype.Component;
import com.bjsxt.bean.User;
import com.bjsxt.user.dao.UserMapper;
import com.bjsxt.user.dao.UserMapper2;
/**
* 配置了这个,<bean
* class="org.mybatis.spring.mapper.MapperScannerConfigurer">,就不在需要配置
* org.mybatis.spring.mapper.MapperFactoryBean了.前者配置1次,自动注入不同的接口类就能够了
* 后者也是通过封装成了单个前者,而且还需要配置多个,所以用前者自动扫面,所有的接口和sql配置文件
* ,在下面的测试类中注入1个接口类,配置的MapperScannerConfigurer会自动的
* 解析出所有的接口提供方法的使用,MapperScannerConfigurer不用配置id了,没有甚么意义了
*
*
*/
@Component
public class Test {
@Autowired
private UserMapper userMapper;
@Autowired
private UserMapper2 userMapper2;
/**
* 下面两个是测试不同的接口类的实现
*/
@org.junit.Test
public void saveUser1() {
ApplicationContext ac = new ClassPathXmlApplicationContext(
"applicationContext.xml");
SqlSessionFactory sf = (SqlSessionFactory) ac
.getBean("sqlSessionFactory");
SqlSession session = sf.openSession();
Test t = (Test) ac.getBean("test");
System.out.println(t.userMapper);
t.userMapper.save(new User(⑴, "连发2", 1));
}
@org.junit.Test
public void saveUser2() {
ApplicationContext ac = new ClassPathXmlApplicationContext(
"applicationContext.xml");
SqlSessionFactory sf = (SqlSessionFactory) ac
.getBean("sqlSessionFactory");
SqlSession session = sf.openSession();
Test t = (Test) ac.getBean("test");
System.out.println(t.userMapper2);
t.userMapper2.save(new User(⑴, "连发2", 1));
}
/**
* application.xml mybatis.spring.mapper.MapperFactoryBean 配置的实现与测试
* 如果有多个接口那末久需要屡次这样的配置,根据id来辨认具体的接口类型
*/
@org.junit.Test
public void saveUser3() {
ApplicationContext ac = new ClassPathXmlApplicationContext(
"applicationContext.xml");
SqlSessionFactory sf = (SqlSessionFactory) ac
.getBean("sqlSessionFactory");
SqlSession session = sf.openSession();
UserMapper mapper = (UserMapper) ac.getBean("UserMapper");
mapper.save(new User(⑴, "连发3", 1));
}
/**
* application.xml中引入了conf.xml配置文件的测试
*/
@org.junit.Test
public void saveUser4() {
ApplicationContext ac = new ClassPathXmlApplicationContext(
"applicationContext.xml");
SqlSessionFactory sf = (SqlSessionFactory) ac
.getBean("sqlSessionFactory");
SqlSession session = sf.openSession(true);
session.insert("com.bjsxt.user.dao.UserMapper.save",new User(⑴, "老婆", 27));
}
public static void main(String[] args) {
new Test().saveUser4();
}
}
3、applicationContext.xml 配置讲授
<?xml version="1.0" encoding="UTF⑻"?>
<beans
xsi:schemaLocation=" http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans⑶.2.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context⑶.2.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx⑶.2.xsd"
xmlns:tx="http://www.springframework.org/schema/tx" xmlns:context="http://www.springframework.org/schema/context"
xmlns:p="http://www.springframework.org/schema/p" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns="http://www.springframework.org/schema/beans">
<context:component-scan base-package="com"></context:component-scan>
<!-- 1. 数据源 : DriverManagerDataSource -->
<bean class="org.springframework.jdbc.datasource.DriverManagerDataSource"
id="datasource">
<property value="com.mysql.jdbc.Driver" name="driverClassName" />
<property value="jdbc:mysql://localhost:3306/mybaits" name="url" />
<property value="root" name="username" />
<property value="123456" name="password" />
</bean>
<!--
2. mybatis的SqlSession的工厂: SqlSessionFactoryBean dataSource /
typeAliasesPackage
-->
<bean class="org.mybatis.spring.SqlSessionFactoryBean" id="sqlSessionFactory">
<property name="dataSource" ref="datasource" />
<property value="com.bjsxt.bean" name="typeAliasesPackage" />
<!--configLocation属性指定mybatis的核心配置文件-->
<property name="configLocation" value="conf.xml"/>
</bean>
<!--
3. mybatis自动扫描加载Sql映照文件和接口
通过类型的注入就能够直接使用其中的方法
-->
<bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
<property value="com.bjsxt.user.dao" name="basePackage" />
<property name="sqlSessionFactory" ref="sqlSessionFactory" />
</bean>
<!-- 专门配置两个接口Mapper 下面两个配置就是,但是这类方式是比较不适用的,要用的话呢就在上面自动扫描的配置中进行 -->
<bean id="UserMapper" class="org.mybatis.spring.mapper.MapperFactoryBean">
<property name="mapperInterface" value="com.bjsxt.user.dao.UserMapper"></property>
<property name="sqlSessionFactory" ref="sqlSessionFactory"></property>
</bean>
<bean id="UserMapper2" class="org.mybatis.spring.mapper.MapperFactoryBean">
<property name="mapperInterface" value="com.bjsxt.user.dao.UserMapper2"></property>
<property name="sqlSessionFactory" ref="sqlSessionFactory"></property>
</bean>
<!-- 4. 事务管理 : DataSourceTransactionManager -->
<bean
class="org.springframework.jdbc.datasource.DataSourceTransactionManager"
id="manager">
<property name="dataSource" ref="datasource" />
</bean>
<!-- 5. 使用声明式事务 -->
<tx:annotation-driven transaction-manager="manager" />
</beans>
4、conf.xml 配置讲授
<?xml version="1.0" encoding="UTF⑻"?>
<!DOCTYPE configuration PUBLIC "-//mybatis.org//DTD Config 3.0//EN" "http://mybatis.org/dtd/mybatis⑶-config.dtd">
<configuration>
<!-- 配置实体类的别名 -->
<typeAliases>
<!-- 下面二者前者是别名,后者是让在xml中省去了包名的写,可直接写入简单类名就好了 -->
<typeAlias type="com.bjsxt.bean.User" alias="_User"/>
<package name="com.bjsxt.bean"/>
</typeAliases>
<!--
development : 开发模式
work : 工作模式
-->
<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:3306/mybaits" />
<property name="username" value="root" />
<property name="password" value="123456" />
</dataSource>
</environment>
</environments>
<mappers>
<mapper resource="com/bjsxt/user/dao/userMapper.xml"/>
<mapper resource="com/bjsxt/user/dao/userMapper2.xml"/>
</mappers>
</configuration>
5、lib 下载地址
Spring集成Mybatis需要的包