使用步骤
-
添加依赖包
<dependency> <groupId>org.mybatis</groupId> <artifactId>mybatis-spring</artifactId> <version>2.0.1</version> </dependency>
-
配置数据源
<bean id="dataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource"> <property name="url" value="${jdbc.url}"/> <property name="driverClassName" value="${jdbc.driver}"/> <property name="username" value="${jdbc.user}"/> <property name="password" value="${jdbc.password}"/> </bean>
-
配置Spring、Mybatis整个的管理Bean
<bean class="org.mybatis.spring.SqlSessionFactoryBean"> <property name="dataSource" ref="dataSource"/> <!-- 指定XML Mapper文件的路径 --> <property name="mapperLocations" value="classpath:/mapper/*"/> </bean>
-
通过mybatis scheme来自动扫描所有的Mapper接口
<beans xmlns="http://www.springframework.org/schema/beans" xmlns:context="http://www.springframework.org/schema/context" xmlns:mybatis="http://mybatis.org/schema/mybatis-spring" 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.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd http://mybatis.org/schema/mybatis-spring http://mybatis.org/schema/mybatis-spring.xsd"> <!-- 自动扫描所有Mapper接口 --> <mybatis:scan base-package="com.lanou3g.spring.mapper" /> </beans>
-
使用
// 经过上面的几步配置后, 我们就可以直接从Spring的IOC容器中获取Mapper操作接口了 public class Application { public static void main(String[] args) throws SQLException { // 1. 初始化IOC容器 ApplicationContext ctx = new ClassPathXmlApplicationContext("applicationContext.xml"); // 2. 从容器中获取Service对象(Mapper对象已经自动注入到Service中了) MessageServiceImpl messageService = ctx.getBean(MessageServiceImpl.class); // 3. 执行查询所有方法 List<Message> messageList = messageService.queryAll(); log.info("" + messageList); } }
示例
jdbc.properties
jdbc.url=jdbc:mysql://localhost:3306/zzj0301?characterEncoding=utf8
jdbc.driver=com.mysql.jdbc.Driver
jdbc.user=root
jdbc.password=root
applicationContext.xml
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:mybatis="http://mybatis.org/schema/mybatis-spring"
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.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context.xsd
http://mybatis.org/schema/mybatis-spring
http://mybatis.org/schema/mybatis-spring.xsd">
<!-- 1、开启注解扫描 -->
<context:component-scan base-package="com.lanou3g.spring"/>
<!-- 2、配置数据源 -->
<context:property-placeholder location="classpath:jdbc.properties" />
<bean id="dataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource">
<property name="url" value="${jdbc.url}"/>
<property name="driverClassName" value="${jdbc.driver}"/>
<property name="username" value="${jdbc.user}"/>
<property name="password" value="${jdbc.password}"/>
</bean>
<!-- 3、配置Spring、Mybatis整个的管理Bean -->
<bean class="org.mybatis.spring.SqlSessionFactoryBean">
<property name="dataSource" ref="dataSource"/>
<property name="mapperLocations" value="classpath:/mapper/*"/>
</bean>
<!-- 4、自动扫描所有Mapper接口 -->
<mybatis:scan base-package="com.lanou3g.spring.mapper" />
</beans>
CityMapper.xml
注意此处xml配置是用genarator工具自动生成的
<?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="com.lanou3g.spring.mapper.CityMapper">
<resultMap id="BaseResultMap" type="com.lanou3g.spring.bean.City">
<id column="id" jdbcType="INTEGER" property="id" />
<result column="cname" jdbcType="VARCHAR" property="cname" />
<result column="pid" jdbcType="INTEGER" property="pid" />
</resultMap>
<delete id="deleteByPrimaryKey" parameterType="java.lang.Integer">
delete from city
where id = #{id,jdbcType=INTEGER}
</delete>
<insert id="insert" parameterType="com.lanou3g.spring.bean.City">
<selectKey keyProperty="id" order="AFTER" resultType="java.lang.Integer">
SELECT LAST_INSERT_ID()
</selectKey>
insert into city (cname, pid)
values (#{cname,jdbcType=VARCHAR}, #{pid,jdbcType=INTEGER})
</insert>
<update id="updateByPrimaryKey" parameterType="com.lanou3g.spring.bean.City">
update city
set cname = #{cname,jdbcType=VARCHAR},
pid = #{pid,jdbcType=INTEGER}
where id = #{id,jdbcType=INTEGER}
</update>
<select id="selectByPrimaryKey" parameterType="java.lang.Integer" resultMap="BaseResultMap">
select id, cname, pid
from city
where id = #{id,jdbcType=INTEGER}
</select>
<select id="selectAll" resultMap="BaseResultMap">
select id, cname, pid
from city
</select>
</mapper>
实体类
注意此处xml配置是用genarator工具自动生成的
public class City {
private Integer id;
private String cname;
private Integer pid;
public Integer getId() {
return id;
}
public void setId(Integer id) {
this.id = id;
}
public String getCname() {
return cname;
}
public void setCname(String cname) {
this.cname = cname == null ? null : cname.trim();
}
public Integer getPid() {
return pid;
}
public void setPid(Integer pid) {
this.pid = pid;
}
@Override
public String toString() {
return "City{" +
"id=" + id +
", cname='" + cname + '\'' +
", pid=" + pid +
"}\n";
}
}
CityMapper.java(接口)
注意此处xml配置是用genarator工具自动生成的
public interface CityMapper {
int deleteByPrimaryKey(Integer id);
int insert(City record);
City selectByPrimaryKey(Integer id);
List<City> selectAll();
int updateByPrimaryKey(City record);
}
CityServiceImpl.java(service层)
@Service
public class CityServiceImpl {
@Autowired
private CityMapper cityMapper;
public List<City> queryAll(){
return cityMapper.selectAll();
}
}
程序运行入口
public class App {
public static void main(String[] args) {
ApplicationContext ac = new ClassPathXmlApplicationContext("applicationContext.xml");
System.out.println(ac.getBean(CityServiceImpl.class).queryAll());
}
}