创建项目等同上一篇非代理方式随笔,只说不一样的部分:
项目结构主要是多了下面红框部分:
配置文件:
主要是dao配置文件中多了Mapper代理java类的扫描包路径:
applicationContext-dao.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:p="http://www.springframework.org/schema/p" xmlns:aop="http://www.springframework.org/schema/aop" xmlns:tx="http://www.springframework.org/schema/tx" 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-4.0.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.0.xsd http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-4.0.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-4.0.xsd http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util-4.0.xsd"> <!-- 加载配置文件 --> <context:property-placeholder location="classpath:resource/*.properties" /> <!-- 引入其他xml --> <import resource="classpath:spring/applicationContext-service.xml"/> <!-- 数据库连接池 --> <bean id="dataSource" class="com.alibaba.druid.pool.DruidDataSource" destroy-method="close"> <property name="url" value="${jdbc.url}" /> <property name="username" value="${jdbc.username}" /> <property name="password" value="${jdbc.password}" /> <property name="driverClassName" value="${jdbc.driver}" /> <property name="maxActive" value="10" /> <property name="minIdle" value="5" /> </bean> <!-- 配置sqlsessionFactory --> <bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean"> <property name="configLocation" value="classpath:mybatis/SqlMapConfig.xml"></property> <property name="dataSource" ref="dataSource"></property> <!-- sql映射文件路径(是否用代理方式都可以在这配置xml文件路径) --> <!-- <property name="mapperLocations" value="classpath*:com/zhangguo/bookstore/mapper/*Mapper.xml"></property> --> <property name="mapperLocations" value="classpath*:mybatis/mapper/*.xml"></property> </bean> <!-- 配置扫描包,加载mapper代理对象(使用代理方式才需要配置这个xxMapper.java的扫描包) --> <bean class="org.mybatis.spring.mapper.MapperScannerConfigurer"> <!-- <property name="basePackage" value="com.taotao.mapper"></property> --> <property name="basePackage" value="mybatis.mapper"></property> </bean> </beans>
其实就是多了下面部分:
<!-- 配置扫描包,加载mapper代理对象(使用代理方式才需要配置这个xxMapper.java的扫描包) -->
<bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
<property name="basePackage" value="mybatis.mapper"></property>
</bean>
在这里其实就是对 BookDAO.java 类的扫描包配置,指定其为对应的代理类。
然后是java代码:
首先,这个就需要针对每个代理类配置一个 xml文件:BookMapper.xml
<?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="mybatis.mapper.BookDAO"> <!--id应该是接口中的方法,结果类型如没有配置别名则应该使用全名称 --> <!--获得所有图书 --> <select id="getAllBooks" resultType="mybatis.pojo.Book"> select id,title,price,publishDate from books </select> <!--获得图书对象通过编号 --> <select id="getBookById" resultType="mybatis.pojo.Book"> select id,title,price,publishDate from books where id=#{id} </select> <!-- 增加 --> <insert id="add"> insert into books(title,price,publishDate) values(#{title},#{price},#{publishDate}) </insert> <!-- 删除 --> <delete id="delete"> delete from books where id=#{id} </delete> <!-- 更新 --> <update id="update"> update books set title=#{title},price=#{price},publishDate=#{publishDate} where id=#{id} </update> </mapper>
注意,开头部分的 命名空间要指定其对应的代理类,这里 是 namespace="mybatis.mapper.BookDAO"
然后是其代理类的java代码:BookDAO.java
package mybatis.mapper; import java.util.List; import org.apache.ibatis.annotations.Param; import mybatis.pojo.Book; /** * 图书数据访问接口 */ public interface BookDAO { /** * 获得所有图书 */ public List<Book> getAllBooks(); /** * 根据图书编号获得图书对象 */ public Book getBookById(@Param("id") int id); /** * 添加图书 */ public int add(Book entity); /** * 根据图书编号删除图书 */ public int delete(int id); /** * 更新图书 */ public int update(Book entity); }
最后是测试类:Test
package test; import java.util.List; import org.junit.BeforeClass; import org.junit.Test; import org.springframework.context.ApplicationContext; import org.springframework.context.support.ClassPathXmlApplicationContext; import mybatis.pojo.Book; import mybatis.service.BookService; import mybatis.service.T1Service; public class T1 { static BookService bookservice; @BeforeClass public static void before(){ ApplicationContext ctx=new ClassPathXmlApplicationContext("classpath:spring/applicationContext-dao.xml"); bookservice=ctx.getBean(BookService.class); } @Test public void testGetAllBooks() { List<Book> books=bookservice.getAllBooks(); System.out.println(111); System.out.println(books); }
//只需看上面部分,当然也可以将其改写在main方法中 public static void main(String[] args) { ApplicationContext ctx=new ClassPathXmlApplicationContext("classpath:spring/applicationContext-dao.xml"); //非代理的方式 T1Service t1Service =ctx.getBean(T1Service.class); t1Service.getBookById(); } }