spring整合mybatis

来源:互联网 发布:川普 知乎 编辑:程序博客网 时间:2024/06/08 04:01

Spring集成mybatis

(1)方式一

step1. 导包。
spring-webmvc,mybatis,mybatis-spring, ojdbc,dbcp,spring-jdbc,junit。

step2. 添加spring的配置文件。
注:集成之后,不再需要mybatis的配置文件了,之前的配置信息 用一个bean(SqlSessionFactoryBean)来代替。

step3.实体类。
step4.映射文件。
step5.Mapper映射器。
step6.配置MapperScannerConfigurer。

注:该bean负责调用SqlSession的getMapper方法,获得 符合Mapper映射器要求的对象。并且会将这些对象放到spring 容器里面(默认的id是首字母小写之后的接口名,比如Mapper 映射器名为EmpDAO,则默认的id是empDAO,也可以使用@Repository 来修改默认的id)。
注:如果只扫描特定的映射器,可以做如下两步。
step1.开发一个注解,比如@MyBatisRepository,并且,将 该注解添加到需要扫描的映射器上面。

step2.给MapperScannerConfigurer注入annotationClass属性值。 

(2)方式二

step1. 导包。
spring-webmvc,mybatis,mybatis-spring, ojdbc,dbcp,spring-jdbc,junit。

step2. 添加spring的配置文件。
注:集成之后,不再需要mybatis的配置文件了,之前的配置信息 用一个bean(SqlSessionFactoryBean)来代替。

step3.实体类。
step4.映射文件。
step5.Mapper映射器。
step6.写映射器的实现类。

注:可以将SqlSessionTemplate注入到实现类中,然后调用 SqlSessionTemplate的方法即可。
step7.配置SqlSessionTemplate。

方法一的代码如下:

实体类:

package entity;public class Dept {private Integer id;private String name;private String loc;public Integer getId() {return id;}public void setId(Integer id) {this.id = id;}public String getName() {return name;}public void setName(String name) {this.name = name;}public String getLoc() {return loc;}public void setLoc(String loc) {this.loc = loc;}@Overridepublic String toString() {return "Dept [id=" + id + ", name=" + name + ", loc=" + loc + "]";}}

spring配置文件:

<?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.xsdhttp://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.2.xsdhttp://www.springframework.org/schema/jdbc http://www.springframework.org/schema/jdbc/spring-jdbc-3.2.xsdhttp://www.springframework.org/schema/jee http://www.springframework.org/schema/jee/spring-jee-3.2.xsdhttp://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.2.xsdhttp://www.springframework.org/schema/data/jpa http://www.springframework.org/schema/data/jpa/spring-jpa-1.3.xsdhttp://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-3.2.xsdhttp://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-3.2.xsdhttp://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util-3.2.xsd"><!-- db.properties --><util:properties id="config" location="classpath:db.properties" /><!-- 连接池 --><bean id="ds" class="org.apache.commons.dbcp.BasicDataSource">  <property name="driverClassName" value="#{config.driver}"/>  <property name="url" value="#{config.url}"/>  <property name="username" value="#{config.username}"></property>  <property name="password" value="#{config.password}"></property></bean><!-- spring集成mybatis不需要mybatis配置文件,配置SqlSessionFactoryBean --><bean id="ssfb" class="org.mybatis.spring.SqlSessionFactoryBean">  <!-- 注入连接池 -->  <!-- 不再使用mybatis自带的连接池,而是使用spring管理的连接池 -->  <property name="dataSource" ref="ds" />    <!-- 映射文件的位置 -->  <property name="mapperLocations" value="classpath:entity/*.xml"/></bean><!-- 配置MapperScannerConfigurer --><!-- 获得符合Mapper映射器要求的对象(相当于调用SqlSession的getMapper()方法)。并且会将这些对象放到spring容器里面(默认的id是首字母小写之后的接口名比如Mapper映射器名为EmpDAO,则默认的id是empDAO,也可以使用@Repository来修改默认的id --><bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">  <!-- 扫描该包及其子包下的所有映射器 -->  <property name="basePackage" value="dao"/></bean><!-- 注意:此配置文件没有配置组件扫描,因为MapperScannerConfigurer会自动调用组件扫描 --></beans>

映射文件:

<?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"><!-- 命名空间为一个字符串,加以区分xml中各个元素 --><mapper namespace="dao.DeptDAO">  <!-- 插入一条数据 -->  <insert id="save" parameterType="entity.Dept" >INSERT INTO de (id,deptName,loc) VALUES (de_seq.nextval,#{name},#{loc})  </insert>  <resultMap type="entity.Dept" id="deptmap">    <result property="name" column="deptName"/>  </resultMap>    <!-- 查询所有记录 -->  <select id="findAll" resultMap="deptmap">  SELECT id,deptName,loc FROM de ORDER BY id  </select>    <!-- 根据id查询一条记录 -->  <select id="findById" resultMap="deptmap" parameterType="int">  SELECT id,deptName,loc FROM de WHERE id = #{id1}  </select>    <!-- 修改一条记录 -->  <update id="modify" parameterType="entity.Dept">      UPDATE de SET deptName = #{name},loc = #{loc} WHERE id = #{id}  </update>  <!-- 删除一条记录 -->  <delete id="delete" parameterType="int">    DELETE FROM de WHERE id = #{id2}  </delete>  </mapper>


映射器:

package dao;import java.util.List;import entity.Dept;public interface DeptDAO {public void save(Dept dept);//插入一条数据public List<Dept> findAll();//查询所有记录public Dept findById(int id);//根据id查询一条记录public void modify(Dept dept);//修改一条记录 public void delete(int id);//删除一条记录}

测试代码:

package test;import java.util.List;import org.junit.Before;import org.junit.Test;import org.springframework.context.ApplicationContext;import org.springframework.context.support.ClassPathXmlApplicationContext;import dao.DeptDAO;import entity.Dept;public class TestCase1 {private DeptDAO dao;@Beforepublic void init(){ApplicationContext ac = new ClassPathXmlApplicationContext("springmvc.xml");dao = ac.getBean("deptDAO",DeptDAO.class);}@Test//插入一条数据public void test1(){Dept dept = new Dept();dept.setName("管理部");dept.setLoc("大连");dao.save(dept);}@Test//查询所有记录public void test2(){List<Dept> list = dao.findAll();for(Dept dept : list)System.out.println(dept);}@Test//根据id查询一条记录public void test3(){Dept dept = dao.findById(21);System.out.println(dept);}@Test//修改一条记录public void test4(){Dept dept = dao.findById(21);dept.setName("保洁部");dept.setLoc("大堂");dao.modify(dept);}@Test//删除一条记录public void test5(){dao.delete(3);}}


原创粉丝点击