Mybatis的Jar包下载:https://pan.baidu.com/s/16P-MGgn53e1EtCL6wQ9VWA 密码:1azq
src/applicationContext.xml(Spring核心配置文件):
<?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:db.properties"/>
<!-- DBCP数据库连接池 -->
<bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource" destroy-method="close">
<property name="driverClassName" value="${jdbc.driver}" />
<property name="url" value="${jdbc.url}" />
<property name="username" value="${jdbc.username}" />
<property name="password" value="${jdbc.password}" />
<property name="maxActive" value="10" />
<property name="maxIdle" value="5" />
</bean>
<!-- Mybatis的工厂 -->
<bean id="sqlSessionFactoryBean" class="org.mybatis.spring.SqlSessionFactoryBean">
<property name="dataSource" ref="dataSource"/>
<!-- Mybatis核心配置文件的位置 -->
<property name="configLocation" value="classpath:sqlMapConfig.xml"/>
</bean>
<!-- Mapper动态代理开发。 注册方式 一(不推荐):所有的Dao层接口都需要在Spring中注册,比较繁琐;可以使用扫描基本包的方式一次性注册多个。 -->
<bean id="userMapper" class="org.mybatis.spring.mapper.MapperFactoryBean">
<property name="sqlSessionFactory" ref="sqlSessionFactoryBean"/>
<!-- 注入Dao层接口(遵循四个原则的接口)。 会自动创建对应Dao层实现类(动态代理)。 -->
<property name="mapperInterface" value="com.xxx.mybatis.mapper.UserMapper"/>
</bean>
<!-- Mapper动态代理开发。 注册方式 二(推荐):扫描基本包下的所有Dao层接口;不需要在Spring中一个一个地注册 -->
<bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
<!-- 会根据类型自动注入Mybatis工厂 -->
<!-- 基本包(也会递归扫描其下的子包) -->
<property name="basePackage" value="com.xxx.mybatis.mapper"/>
</bean>
</beans>
src/sqlMapConfig.xml(Mybatis核心配置文件):
<?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>
<!-- 为类型设置别名 -->
<typeAliases>
<package name="com.xxx.mybatis.pojo" />
</typeAliases>
<!-- 加载外部实体类的Sql映射文件 -->
<mappers>
<package name="com.xxx.mybatis.mapper"/>
</mappers>
</configuration>
UserMapper.java(遵循四个规则的Dao层接口):
package com.xxx.mybatis.mapper;
import com.xxx.mybatis.pojo.User;
//遵循四个原则的Dao层接口
public interface UserMapper {
//遵循四个原则:
//UserMapper.xml中配置的命名空间要与该接口的全类名保持一致(com.xxx.mybatis.mapper.UserMapper)
//接口中的方法名 == UserMapper.xml中配置的sql语句的id名
//返回值类型 与 UserMapper.xml文件中配置的返回值类型(resultType)要一致
//方法的输入参数类型 与UserMapper.xml中配置的输入参数的类型(parameterType)要一致
public User findUserById(Integer id);
}
UserMapper.xml(Sql配置文件):
<?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">
<!-- 配置原生Sql语句 -->
<mapper namespace="com.xxx.mybatis.mapper.UserMapper"> <!-- 命名空间指定为对应Dao层接口的全类名 -->
<!-- 根据ID查询用户 -->
<select id="findUserById" parameterType="Integer" resultType="User">
select * from user where id = #{v}
</select>
</mapper>
Test.java(测试类):
package com.xxx.mybatis.junit;
import org.junit.Test;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import com.xxx.mybatis.mapper.UserMapper;
import com.xxx.mybatis.pojo.User;
public class Test {
@Test
public void testMapper() throws Exception {
//创建Spring容器
ApplicationContext ac = new ClassPathXmlApplicationContext("applicationContext.xml");
UserMapper mapper = ac.getBean(UserMapper.class); //动态代理,自动创建Dao层实现类。
//也可以通过Spring容器中配置的bean的id获取Dao层实现类。 (如果用扫描的方式注册的Dao层接口,那么就没有id,不能用这种方式获取实现类)
//UserMapper mapper = (UserMapper) ac.getBean("userMapper");
User user = mapper.findUserById(10);
System.out.println(user);
}
}
db.properties:
jdbc.driver=com.mysql.jdbc.Driver
jdbc.url=jdbc:mysql://localhost:3306/mybatis?characterEncoding=utf-8
jdbc.username=root
jdbc.password=123