Spring MyBatis整合之Mapper代理和批量扫描

目录结构

 

数据库配置文件db.properties

jdbc.driver=com.mysql.jdbc.Driver
jdbc.url=jdbc:mysql://localhost/mytest
jdbc.username=root
jdbc.password=123456

 

MySQL数据库建表语句

CREATE TABLE `user` (
  `uid` int(15) NOT NULL AUTO_INCREMENT,
  `username` varchar(20) NOT NULL,
  `password` varchar(30) NOT NULL,
  `name` varchar(30) NOT NULL,
  `sex` varchar(5) NOT NULL,
  PRIMARY KEY (`uid`)
) ENGINE=InnoDB AUTO_INCREMENT=112 DEFAULT CHARSET=utf8;

 

日志log4j.properties

log4j.rootLogger=DEBUG,stdout
log4j.appender.stdout=org.apache.log4j.ConsoleAppender
log4j.appender.stdout.layout=org.apache.log4j.PatternLayout
log4j.appender.stdout.layout.ConversionPattern=%5p [%t] - %m%n

 

JavaBean类User.java

public class User {
	private int uid; 
	private String username;
	private String password;
    private String name;
    private String sex;
	public int getUid() {
		return uid;
	}
	public void setUid(int uid) {
		this.uid = uid;
	}
	public String getUsername() {
		return username;
	}
	public void setUsername(String username) {
		this.username = username;
	}
	public String getPassword() {
		return password;
	}
	public void setPassword(String password) {
		this.password = password;
	}
	public String getName() {
		return name;
	}
	public void setName(String name) {
		this.name = name;
	}
	public String getSex() {
		return sex;
	}
	public void setSex(String sex) {
		this.sex = sex;
	}
	@Override
	public String toString() {
		return "User [uid=" + uid + ", username=" + username + ", password=" + password + ", name=" + name + ", sex="
				+ sex + "]";
	}
}

 

Spring全局配置文件applicationContext.xml

<beans xmlns="http://www.springframework.org/schema/beans"
	xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
	xmlns:mvc="http://www.springframework.org/schema/mvc"
	xmlns:context="http://www.springframework.org/schema/context"
	xmlns:aop="http://www.springframework.org/schema/aop"
	xmlns:tx="http://www.springframework.org/schema/tx"
	xsi:schemaLocation="http://www.springframework.org/schema/beans 
		http://www.springframework.org/schema/beans/spring-beans-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/context 
		http://www.springframework.org/schema/context/spring-context-3.2.xsd 
		http://www.springframework.org/schema/aop 
		http://www.springframework.org/schema/aop/spring-aop-3.2.xsd 
		http://www.springframework.org/schema/tx 
		http://www.springframework.org/schema/tx/spring-tx-3.2.xsd">
	<!-- 加载数据库配置文件 -->
	<context:property-placeholder
		location="classpath:db.properties" />

	<!-- 配置数据源 -->
	<bean id="dataSource"
		class="org.springframework.jdbc.datasource.DriverManagerDataSource">
		<property name="driverClassName" value="${jdbc.driver}"></property>
		<property name="url" value="${jdbc.url}"></property>
		<property name="username" value="${jdbc.username}"></property>
		<property name="password" value="${jdbc.password}"></property>
	</bean>
	
	
	<!-- sqlSessionFactory -->
	<bean id="sqlSessionFactory"
		class="org.mybatis.spring.SqlSessionFactoryBean">
		<!-- 加载mybatis配置文件 -->
		<property name="configLocation"
			value="mybatis/SqlMapConfig.xml"></property>
		<!-- 配置数据源 -->
		<property name="dataSource" ref="dataSource"></property>
	</bean>
	
	
	<bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
        <!-- 扫描com.it.mapper包下的.xml文件 -->
		<property name="basePackage" value="com.it.mapper" />
        <!-- 引入sqlSessionFactory -->
		<property name="sqlSessionFactoryBeanName" value="sqlSessionFactory" />
	</bean>
	
</beans>

 

MyBatis全局配置文件SqlMapConfig.xml

<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE configuration
 PUBLIC "-//mybatis.org//DTD Config 3.0//EN"
 "http://mybatis.org/dtd/mybatis-3-config.dtd">
 <configuration>
 	<settings>
 		<!-- 开启二级缓存 -->
 		<setting name="cacheEnabled" value="true"/>
 	</settings>
 	<mappers>
 	</mappers>
 
 </configuration>

 

Mapper接口UserMapper.java

public interface UserMapper {
    public User findUserById(int id)throws Exception;

}


Mapper映射文件UserMapper.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="com.it.mapper.UserMapper">
	
	<cache />
	<resultMap type="com.it.po.User" id="userMap">
		<id property="uid" column="uid"/>
		<result property="name" column="name"/>
		<result property="username" column="username"/>
		<result property="password" column="password"/>
		<result property="sex" column="sex"/>
	</resultMap>
	<select id="findUserById" parameterType="int" resultMap="userMap" flushCache="true">
		select * from user where uid=#{id};
	</select>
	
</mapper>

 

 

测试类Test.java

package test;

import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import com.it.mapper.UserMapper;
import com.it.po.User;

public class Test {
	
	@org.junit.Test
	public void findUserById() throws Exception {
		ApplicationContext applicationContext =new ClassPathXmlApplicationContext("classpath:spring/applicationContext.xml");
		UserMapper userMapper =(UserMapper) applicationContext.getBean("userMapper");
		User user1 =userMapper.findUserById(94);
		System.out.println(user1);
		

	}

}

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值