Spring+MyBatis
Posted 阴翳公子
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Spring+MyBatis相关的知识,希望对你有一定的参考价值。
spring.xml整合
SqlSessionFactoryBean组件配置
为整合应用提供SQLSession对象
MapperScannerConfigurer组件配置
根据指定的包批量的去扫描Mapper接口,生成对应的实例
如果指定某个包下并不完全是我们定义的Mapper接口,此时可以使用MapperScannerConfigurer的两个属性来缩小接口
的实例化范围,一个是使用annotationClass,一个是markInterface
二者不可以同时使用,推荐使用注解标记
<?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" > <!-- 加载属性配置文件 --> <util:properties id="db" location="classpath:db.properties" /> <!-- 定义数据源 --> <bean id="ds" class="org.apache.commons.dbcp.BasicDataSource"> <property name="driverClassName" value="#{db.driver}"></property> <property name="url" value="#{db.url}"></property> <property name="username" value="#{db.user}"></property> <property name="password" value="#{db.pwd}"></property> </bean> <!-- 定义SQLSessionFactoryBean组件 --> <bean id="sqlSessionFactoryBean" class="org.mybatis.spring.SqlSessionFactoryBean" > <!-- 没有了MyBatis的主配置文件 --> <!-- 需要指定连接资源 --> <property name="dataSource" ref="ds"></property> <!-- 需要指定映射文件 --> <property name="mapperLocations" value="classpath:com/xms/entity/mapper/*.xml"></property> </bean> <!-- 定义MapperScannerrConfigurer扫描组件 --> <bean class="org.mybatis.spring.mapper.MapperScannerConfigurer" > <!-- 指定Mapper接口扫描包 --> <property name="basePackage" value="com.xms.dao" ></property> <!-- 手动指定SqlSessionFactory对象 --> <!-- sqlSessionFactory属性可以不用指定,它会以Autowired方式自动注入 --> <property name="sqlSessionFactory" ref="sqlSessionFactoryBean" ></property> </bean> </beans>
EmpMapper.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"> <mapper namespace="com.xms.dao.EmpMapper"> <!-- 查看全部emp表记录 --> <select id="findAll" resultType="com.xms.entity.Emp"> select*from emp </select> <!-- 按编号查看记录 --> <select id="findByEmpno" resultType="com.xms.entity.Emp" parameterType="java.lang.Integer"> select*from emp where empno=#{empno} </select> <!-- 增加一条记录 --> <insert id="save" parameterType="com.xms.entity.Emp" > insert into emp values(#{empno},#{ename},#{salary},#{bonus},#{hiredate},#{deptno}) </insert> <!-- 修改一条记录 --> <update id="update" parameterType="com.xms.entity.Emp"> update emp set ename=#{ename},bonus=#{bonus},hiredate=#{hiredate},deptno=#{deptno} where empno=#{empno} </update> <!-- 删除一条记录 --> <delete id="delete" parameterType="com.xms.entity.Emp"> delete from emp where empno=#{empno} </delete> </mapper>
TestCase.java
package com.xms.test; import java.util.List; import org.junit.Test; import org.springframework.context.ApplicationContext; import org.springframework.context.support.ClassPathXmlApplicationContext; import com.xms.dao.EmpMapper; import com.xms.entity.Emp; public class TestCase { ApplicationContext ac=new ClassPathXmlApplicationContext("spring.xml");//实例化容器 EmpMapper empMapper=ac.getBean("empMapper", EmpMapper.class);//查找反射对象 @Test public void testOne(){ List<Emp> emps=empMapper.findAll(); for(Emp emp:emps){ System.out.println(emp.getEname()+":"+emp.getBonus()); } } }
EmpMapper.java
package com.xms.dao; import java.util.List; import com.xms.entity.Emp; public interface EmpMapper { List<Emp> findAll(); Emp findByEmpno(int empno); void save(Emp emp); void update(Emp emp); void delete(Emp emp); }
jar包
spring ioc 5个 + spring jdbc 2个 + spring aop 1个 + mysql 1个 + MyBatis 2个 + DBCP 3个
以上是关于Spring+MyBatis的主要内容,如果未能解决你的问题,请参考以下文章
markdown [mybatis参考]关于mybatis #mybatis的一些片段