<dependency>
<groupId>org.mybatis</groupId>
<artifactId>mybatis-spring</artifactId>
<version>1.2.2</version>
</dependency>
<dependency>
<groupId>commons-dbcp</groupId>
<artifactId>commons-dbcp</artifactId>
<version>1.4</version>
</dependency>
spring和mybatis整合时,dao的开发是围绕mybatis的,由于mybatis提供了mapper代理机制。
整合配置文件:需要注意的是,数据库连接是配置在spring配置文件中的,mybatis的配置文件(即Mapper的配置文件)可以要也不可以不要,主要看你以什么方式配置,为了程序的简洁可读性着想,个人觉得还是采用注解的方式,就不要单独在定义一个Mapper的配置文件了。以注解的方式配置文件如下
<?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:aop="http://www.springframework.org/schema/aop"
xmlns:mybatis="http://mybatis.org/schema/mybatis-spring"
xmlns:tx="http://www.springframework.org/schema/tx"
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://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop.xsd
http://www.springframework.org/schema/mvc
http://www.springframework.org/schema/mvc/spring-mvc.xsd
http://www.springframework.org/schema/jee
http://www.springframework.org/schema/jee/spring-jee.xsd
http://www.springframework.org/schema/tx
http://www.springframework.org/schema/tx/spring-tx.xsd
http://mybatis.org/schema/mybatis-spring
http://mybatis.org/schema/mybatis-spring.xsd ">
<context:component-scan base-package="spring.mybatis"> </context:component-scan>
<mybatis:scan base-package="spring.mybatis" />
<bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource"
destroy-method="close">
<property name="driverClassName" value="${jdbc.driverClassName}" />
<property name="url" value="${jdbc.url}" />
<property name="username" value="${jdbc.username}" />
<property name="password" value="${jdbc.password}" /><!-- 其实这些参数也可以直接写值,不用.properties文件也行 -->
</bean>
<bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
<!-- 数据库连接池 -->
<property name="dataSource" ref="dataSource" />
<!-- 加载mybatis的全局配置文件 -->
</bean>
<bean id="sqlSession" class="org.mybatis.spring.SqlSessionTemplate">
<constructor-arg index="0" ref="sqlSessionFactory" />
</bean>
<context:property-placeholder location="db.properties" />
</beans>
这里需要注意的有xmlns:mybatis="http://mybatis.org/schema/mybatis-spring"
http://mybatis.org/schema/mybatis-spring
http://mybatis.org/schema/mybatis-spring.xsd "
<mybatis:scan base-package="spring.mybatis" />
这几行代码的作用是为了让Mybatis自动去发现Mapper就是通过注解来注册。尤其是当mapper很多时,就不用写mapper的xml了。还需要注意的是这几行代码<bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
<property name="dataSource" ref="dataSource" />
</bean>
<bean id="sqlSession" class="org.mybatis.spring.SqlSessionTemplate">
<constructor-arg index="0" ref="sqlSessionFactory" />
</bean>
这就是在MyBatis中的session。在Mybatis中是通过sqlsessionfactorybuilder来创建而在Mybatis-spring中是通过sqlsessionFactory Bean来代替(即上面的配置代码)
下面是我的db.properties。其实也可以直接在配置文件中写进去。
jdbc.driverClassName=com.mysql.jdbc.Driver
jdbc.url=jdbc:mysql://localhost:3306/test
jdbc.username=root
jdbc.password=123
如果说通过xml写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">
<!--命名空间,用于隔离sql语句,后面会讲另一层非常重要的作用。-->
<mapper namespace="com.aust.dao.UserMapper">
<!--根据id查询出用户信息(查询一条数据)-->
<select id="findUserById" parameterType="int" resultType="com.aust.model.User">
SELECT * FROM user WHERE id=#{id}
</select>
</mapper>
接下来应该定义一个类了。用来和数据表来映射的简单类(属性要和表中字段名最好一样。否则需要在接口(mapper)中加映射代码 下面的代码会有说名)
package spring.mybatis;
public class People {
private int id;
private String first_name;
private String last_name;
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getFirstname() {
return first_name;
}
public void setFirstname(String first_name) {
this.first_name = first_name;
}
public String getLastname() {
return last_name;
}
public void setLastname(String last_name) {
this.last_name = last_name;
}
}
接下来就是DAO接口了。代码如下
package spring.mybatis;
import java.util.List;
import org.apache.ibatis.annotations.Result;
import org.apache.ibatis.annotations.Results;
import org.apache.ibatis.annotations.Select;
import org.springframework.stereotype.Repository;
public interface MybatisPeopleDao {
@Select("select * from people where first_name=#{firstName}")//也可以通过xml配置这个Mapper不顾那样的话就比较麻烦不简洁
public People getPeople(String firstName);
// 如果说POJO类中属性名和数据库表中字段名不一样的话可以采用下面这种发放来映射。但最好不要这样。
//而且下面这些代码要放在被用的代码之上
// @Results({
// @Result(property ="id", column="id"),
// @Result(property ="firstname", column="first_name"),
// @Result(property ="lastname", column="last_name")
// })
@Select("Select * from people")
public List<People> getPeopleList();
}
接下来就是最后一步的测试类了
package spring.mybatis;
import java.util.List;
import org.springframework.context.ApplicationContext;
import org.springframework.context.ConfigurableApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
public class TestData {
public static void main(String[] args) {
ApplicationContext ctx=new ClassPathXmlApplicationContext("springMybatis.xml");
MybatisPeopleDao dao= ctx.getBean("mybatisPeopleDao", MybatisPeopleDao.class);
List<People> list= dao.getPeopleList();
for(People people:list){
System.out.println(people.getFirstname()+" "+people.getLastname()+people.getId());
}
People pp= dao.getPeople("MeiMei");
System.out.println(pp.getId());
((ConfigurableApplicationContext) ctx).close();
}
}
我的数据库表名是people 字段有id first_name last_name .完成以上就可以了,就完成了spring和mybatis的整合。