步骤
1.创建一个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>
<!-- 设置别名 -->
<typeAliases>
<!-- 2. 指定扫描包,会把包内所有的类都设置别名,别名的名称就是类名,大小写不敏感 -->
<package name="com.itheima.mybatis.pojo" />
</typeAliases>
</configuration>
2.创建一个applicationContext.xml文件
加载配置文件db.properties
配置数据库链接池
配置sqlSessionFactory 工厂
配置mybatis 的核心文件
配置数据源
<?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" />
<!-- 数据库连接池 -->
<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的工厂 SqlSessionFactory -->
<bean id="sqlSessionFactoryBean" class="org.mybatis.spring.SqlSessionFactoryBean">
<!-- 配置mybatis核心配置文件 -->
<property name="configLocation" value="classpath:sqlMapConfig.xml" />
<!-- 配置数据源 -->
<property name="dataSource" ref="dataSource" />
</bean>
</beans>
3.创建pojo类(User)
public class User implements Serializable {
private static final long serialVersionUID = 1L;
private Integer id;
private String username;
private String sex;
private Date birthday;
private String address;
set\get....
4.实现UserMapper接口
public interface UserMapper {
public User selectUserById(Integer id);
}
5.实现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.itheima.mybatis.mapper.UserMapper">
<select id="selectUserById" parameterType="Integer" resultType="User">
select * from user where id = #{id}
</select>
</mapper>
6.在sqlMapConfig.xml中加载UserMapper.xml
<mappers>
<package name="com.itheima.mybatis.mapper"/>
</mappers>
7.在applicationContext.xml中配置mapper
- 法一:
<bean id="userMapper" class="org.mybatis.spring.mapper.MapperFactoryBean">
<property name="sqlSessionFactory" ref="sqlSessionFactoryBean"/>
<property name="mapperInterface" value="com.itheima.mybatis.mapper.UserMapper"/>
</bean>
- 法二:扫描(企业中经常使用)
<bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
<!-- 指定基本包 -->
<property name="basePackage" value="com.itheima.mybatis.mapper"/>
</bean>
8.测试
public void testMapper() {
ApplicationContext ac = new ClassPathXmlApplicationContext("applicationContext.xml");
//法一
UserMapper userMapper = (UserMapper) ac.getBean("userMapper");
User selectUserById = userMapper.selectUserById(10);
System.out.println(selectUserById);
//法二
UserMapper mapper = ac.getBean(UserMapper.class);
User selectUserById2 = mapper.selectUserById(10);
System.out.println(selectUserById2);
}