MyBatis + Spring 整合示例代码
springmvc.xml
<?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:jdbc="http://www.springframework.org/schema/jdbc"
xmlns:jee="http://www.springframework.org/schema/jee"
xmlns:tx="http://www.springframework.org/schema/tx"
xmlns:aop="http://www.springframework.org/schema/aop"
xmlns:mvc="http://www.springframework.org/schema/mvc"
xmlns:util="http://www.springframework.org/schema/util"
xmlns:jpa="http://www.springframework.org/schema/data/jpa"
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/jdbc http://www.springframework.org/schema/jdbc/spring-jdbc-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/aop http://www.springframework.org/schema/aop/spring-aop-3.2.xsd
http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-3.2.xsd
http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util-3.2.xsd">
<!-- 组件扫描 -->
<context:component-scan base-package="mapper"/>
<util:properties id="jdbc"
location="classpath:db.properties"/>
<!-- 配置数据源(DataSource) -->
<bean id="ds" class="org.apache.commons.dbcp.BasicDataSource"
destroy-method="close">
<property name="driverClassName" value="#{jdbc.driver}"/>
<property name="url" value="#{jdbc.url}"/>
<property name="username" value="#{jdbc.user}"/>
<property name="password" value="#{jdbc.pwd}"/>
</bean>
<!-- MyBatis配置 -->
<!-- 管理SqlSessionFactory -->
<bean id="sqlSessionFactory"
class="org.mybatis.spring.SqlSessionFactoryBean">
<property name="dataSource" ref="ds"/>
<property name="mapperLocations" value="classpath:mapper/*.xml"/>
</bean>
<!-- 管理Mapper对象 -->
<!-- basePackage的值指向Mapper接口的包 -->
<bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
<property name="basePackage" value="mapper"/>
<!-- 利用annotationClass属性定义注解,只有被注解的Mapper接口才能被创建对象 -->
<property name="annotationClass" value="mapper.MapperBean"/>
</bean>
</beans>
DeptMapper.java
package mapper;
import java.util.List;
import java.util.Map;
import entity.Dept;
@MapperBean
public interface DeptMapper {
List<Dept> findAll();
Dept findDeptById(Integer id);
void addDept(Dept dept);
void deleteDept(Dept dept);
void updateDept(Dept dept);
List<Map<String,Object>> findDeptAll();
}
DeptMapper.xml
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE mapper PUBLIC "-//ibatis.apache.org//DTD Mapper 3.0//EN"
"http://ibatis.apache.org/dtd/ibatis-3-mapper.dtd">
<!-- DeptMapper.xml -->
<mapper namespace="mapper.DeptMapper">
<!-- id:方法名,resultType:返回值中对象的类型 -->
<!-- SQL语句后面不能写分号,否则出错 -->
<select id="findAll" resultType="entity.Dept">
select deptNo,dname,loc from dept_qxl
</select>
<select id="findDeptById" resultType="entity.Dept"
parameterType="java.lang.Integer">
select deptNo,dname,loc from dept_qxl where deptNo=#{id}
</select>
<insert id="addDept" parameterType="entity.Dept">
insert into dept_qxl (deptNo,dname,loc) values(dept_seq_qxl.nextval,#{dname},#{loc})
</insert>
<delete id="deleteDept" parameterType="entity.Dept">
delete from dept_qxl where deptNo=#{deptNo}
</delete>
<update id="updateDept" parameterType="entity.Dept">
update dept_qxl set dname=#{dname},loc=#{loc} where deptNo=#{deptNo}
</update>
<!-- 此处须用双引号 -->
<select id="findDeptAll" resultType="java.util.Map">
select dname as "dname",loc as "loc" from dept_qxl
</select>
</mapper>
Dept.java
package entity;
import java.io.Serializable;
public class Dept implements Serializable{
private Integer deptNo;
private String dname;
private String loc;
public Dept(){
}
public Integer getDeptNo() {
return deptNo;
}
public void setDeptNo(Integer deptNo) {
this.deptNo = deptNo;
}
public String getDname() {
return dname;
}
public void setDname(String dname) {
this.dname = dname;
}
public String getLoc() {
return loc;
}
public void setLoc(String loc) {
this.loc = loc;
}
@Override
public String toString() {
return "Dept [deptNo=" + deptNo + ", dname=" + dname + ", loc=" + loc
+ "]";
}
}
MapperBean.java
package mapper;
/**
* 自定义注解,用于标识哪些Mapper接口被扫描
* @author soft01
*
*/
public @interface MapperBean {
}
TestCase.java
package test;
import java.util.List;
import mapper.DeptMapper;
import org.junit.Test;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import entity.Dept;
public class TestCase {
ApplicationContext ac =
new ClassPathXmlApplicationContext("springmvc.xml");
@Test
public void testDbProperties(){
System.out.println(ac.getBean("jdbc"));
}
@Test
public void testMapperBean(){
DeptMapper dm = ac.getBean("deptMapper",DeptMapper.class);
//System.out.println(dm);
List<Dept> list = dm.findAll();
for(Dept d : list){
System.out.println(d);
}
}
}