Mybatis3.2.1使用例三:Mapper方式集成Spring、配置文件提供SQL

来源:互联网 发布:万网 域名 编辑:程序博客网 时间:2024/06/18 09:21

上例介绍了使用注解而不需要mybatis配置文件的方式来提供SQL,不过在一个或几个配置文件中集中管理所有SQL语句也是另外一种便利的方式,下例介绍怎样使用配置文件来提供SQL语句,同样还是使用Mapper的方式进行数据库操作。


(1) 承载数据的User bean:com/mybatis/demo3/User.java

package com.mybatis.demo3;public class User {/** 用户ID */private int id;/** 用户名称 */private String name;/** 用户密码 */private String password;public int getId() {return id;}public void setId(int id) {this.id = id;}public String getName() {return name;}public void setName(String name) {this.name = name;}public String getPassword() {return password;}public void setPassword(String password) {this.password = password;}}
(2) Mapper接口类:com/mybatis/demo3/UserMapper.java

(只需提供接口方法,应用中mybatis会使用动态反射的方式提供查询的实际逻辑;该Mapper中没有通过注解提供所使用的SQL语句,SQL语句挪到配置文件中进行配置)

package com.mybatis.demo3;import org.apache.ibatis.annotations.Param;public interface UserMapper {// 这里不使用注解,使用的SQL语句来自于配置文件UserMappers.xmlpublic User findByName(@Param("name") String name);}
(3) 数据源properties文件:com/mybatis/demo2/mysql.properties
driver=com.mysql.jdbc.Driverurl=jdbc:mysql://127.0.0.1:3306/bookstoreusername=rootpassword=123456
(4) Spring的配置文件:com/mybatis/demo3/applicationContext.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"xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd"><bean class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer"><property name="locations" value="classpath:com/mybatis/demo3/mysql.properties"/></bean><!-- 数据源 --><bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource"destroy-method="close"><property name="driverClassName" value="${driver}" /><property name="url" value="${url}" /><property name="username" value="${username}" /><property name="password" value="${password}" /></bean><!-- 创建SqlSessionFactory,需指定数据源,property名称必须为dataSource --><bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean"><property name="dataSource" ref="dataSource" /><property  name="configLocation"  value="classpath:com/mybatis/demo3/mybatis_config.xml"/></bean><!--创建数据映射器Mapper,属性mapperInterface的value必须为接口类--><bean id="userMapper" class="org.mybatis.spring.mapper.MapperFactoryBean"><property name="mapperInterface" value="com.mybatis.demo3.UserMapper" /><property name="sqlSessionFactory" ref="sqlSessionFactory" /></bean><!-- 数据访问DAO,在DAO中使用Mapper来查数据 --><bean id="userDao" class="com.mybatis.demo3.UserDaoImpl"><property name="userMapper" ref="userMapper"/></bean></beans>
(5) Mybatis配置文件:com/mybatis/demo3/mybatis_config.xml

(与Spring集成后,该配置文件很多属性不能用了,比如setting、environment等,主要是使用typeAliases和mappers)

<?xml version="1.0" encoding="UTF-8" ?><!DOCTYPE configurationPUBLIC "-//mybatis.org//DTD Config 3.0//EN" "http://mybatis.org/dtd/mybatis-3-config.dtd"><configuration><typeAliases><typeAlias alias="User" type="com.mybatis.demo3.User" /></typeAliases><mappers><mapper resource="com/mybatis/demo3/UserMappers.xml" /></mappers></configuration>

(6) Mapper配置文件:com/mybatis/demo3/UserMappers.xml

(提供SQL的详细配置,其中select/insert/update/delete语句的id都必须与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"><!-- 用Mapper的时候必须配置namespace,且值为Mapper的类路径;直接使用SqlSession时可配可不配 --><mapper namespace="com.mybatis.demo3.UserMapper"><resultMap id="userMap" type="User"><id property="id" column="id" /><result property="name" column="name" /><result property="password" column="password" /></resultMap><select id="findByName" parameterType="String" resultMap="userMap">select * from users where name=#{name}</select></mapper>  
(7) DAO接口:com/mybatis/demo3/UserDao.java

(该接口不是必须有,但在WEB环境中最好能把它接口化使得依赖更小;当然本例并不是WEB环境中使用的,但除了测试类外都可以直接移植到WEB中)

package com.mybatis.demo3;public interface UserDao {public User getUserByName(String name);}
(8) DAO实现类:com/mybatis/demo3/UserDaoImpl.java

(通过Spring注入的方式获取到Mapper,然后使用Mapper进行数据库操作)

package com.mybatis.demo3;public class UserDaoImpl implements UserDao {/** 该映射器需要与applicationContext.xml中配置的bean属性一致,并且要用对应的getter/setter */private UserMapper userMapper;@Overridepublic User getUserByName(String name) {return getUserMapper().findByName(name);}public UserMapper getUserMapper() {return userMapper;}public void setUserMapper(UserMapper userMapper) {this.userMapper = userMapper;}}
(9) 测试类:com/mybatis/demo3/UserManagerPrgm.java

package com.mybatis.demo3;import org.springframework.context.ApplicationContext;import org.springframework.context.support.ClassPathXmlApplicationContext;public class UserManagerPrgm {public static void main(String[] args) {String configLocation = "com/mybatis/demo3/applicationContext.xml";ApplicationContext context = new ClassPathXmlApplicationContext(configLocation);UserDao dao = (UserDao)context.getBean("userDao");User user = dao.getUserByName("Joe");System.out.println(user);}}

相比注解的方式,这里多了一个Mapper.xml配置文件(对于Mybatis的配置文件在使用注解也是可以使用的,主要是提供一个整体的配置),当然Mapper的配置文件可以有多个(建议按模块进行分类以便维护)。该方式的好处是:可以在独立的配置文件中管理SQL语句。




原创粉丝点击