目录
1.SqlSessionFactoryBean源码
2.配置beans.xml
3.配置mybatis-config.xml(全局配置类)
4.映射器(接口+xml)配置
5.其它配置(jdbc.properties、log4j.properties、pojo、测试类)
6.配置pom.xml
1.SqlSessionFactoryBean源码
SqlSessionFactory是产生SqlSession的基础,因此配置SqlSessionFactory十分关键。MyBatis-Spring项目提供了SqlSessionFactoryBean去支持SqlSessionFactory配置
SqlSessionFactoryBean源码
public class SqlSessionFactoryBean implements FactoryBean<SqlSessionFactory>, InitializingBean, ApplicationListener<ApplicationEvent> {
// 日志
private static final Log LOGGER = LogFactory.getLog(SqlSessionFactoryBean.class);
// MyBatis配置文件
private Resource configLocation;
// Configuration对象
private Configuration configuration;
// Mapper配置路径
private Resource[] mapperLocations;
// 数据库
private DataSource dataSource;
// 事务管理器
private TransactionFactory transactionFactory;
// 配置属性
private Properties configurationProperties;
// SqlSessionFactoryBuilder
private SqlSessionFactoryBuilder sqlSessionFactoryBuilder = new SqlSessionFactoryBuilder();
// SqlSessionFactory
private SqlSessionFactory sqlSessionFactory;
// enviroment
private String environment = SqlSessionFactoryBean.class.getSimpleName();
// 当加载后,是否检测所有MyBatis的映射语句加载完全,默认为false
private boolean failFast;
// 插件
private Interceptor[] plugins;
// 类型转换器 typeHandlers
private TypeHandler<?>[] typeHandlers;
// 类型转换包,用于扫描加载
private String typeHandlersPackage;
// 别名
private Class<?>[] typeAliases;
// 别名包,用于扫描加载
private String typeAliasesPackage;
// 当扩展了上面Class类后,就生成别名,如果没有配置typeAliasesPackage则不会生效
private Class<?> typeAliasesSuperType;
// 数据库厂商标识
private DatabaseIdProvider databaseIdProvider;
// unix文件操作
private Class<? extends VFS> vfs;
// 缓存
private Cache cache;
private ObjectFactory objectFactory;
// 对象包装器
private ObjectWrapperFactory objectWrapperFactory;
/*****setter and other methods ******/
}
目录结构
2.配置beans.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"
xmlns:context="http://www.springframework.org/schema/context"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd">
<!--加载properties配置文件-->
<context:property-placeholder location="classpath:jdbc.properties"/>
<!--配置数据源-->
<bean name="dataSource" class="com.alibaba.druid.pool.DruidDataSource">
<property name="driverClassName" value="${db.driverClassName}"/>
<property name="url" value="${db.url}"/>
<property name="username" value="${db.username}"/>
<property name="password" value="${db.password}"/>
</bean>
<!--配置数据源事务管理器-->
<bean class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
<property name="dataSource" ref="dataSource"/>
</bean>
<!--加载mybatis全局配置文件,生成sqlSessionFactory对象-->
<bean name="SqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
<!--数据库-->
<property name="dataSource" ref="dataSource"/>
<!--MyBatis全局配置文件-->
<property name="configLocation" value="classpath:mybatis-config.xml"/>
<!--Mapper映射器配置路径-->
<property name="mapperLocations" value="classpath*:mappers/*Mapper.xml"/>
</bean>
<!--通过扫描配置Mapper的类-->
<bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
<property name="basePackage" value="cn.whc.April_11.mapper"/>
<!--指定在Spring中定义SqlSessionFactory的Bean名称-->
<property name="SqlSessionFactoryBeanName" value="SqlSessionFactory"/>
<!--指定标注才扫描成为Mapper-->
<property name="annotationClass" value="org.springframework.stereotype.Repository"/>
</bean>
</beans>
3.配置mybatis-config.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"/>
<!--允许JDBC支持生成的键-->
<setting name="useGeneratedKeys" value="true"/>
<!--配置默认的执行器, REUSE执行器重用预处理语句-->
<setting name="defaultExecutorType" value="REUSE"/>
<!--全局启动延迟加载-->
<setting name="lazyLoadingEnabled" value="true"/>
<!--关闭层级加载-->
<setting name="aggressiveLazyLoading" value="false"/>
<!--设置超时时间,它决定驱动等待一个数据库响应的时间-->
<setting name="defaultStatementTimeout" value="25000"/>
</settings>
</configuration>
4.映射器(接口+xml)配置
接口
@Repository
public interface RoleMapper {
int insertRole(Role role);
Role getRole(@Param("id") Long id);
int updateRole(Role role);
int deleteRole(@Param("id") Long id);
}
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="cn.whc.April_11.mapper.RoleMapper">
<insert id="insertRole" useGeneratedKeys="true" keyProperty="id">
insert into t_role(role_name, note) values (#{roleName}, #{note})
</insert>
<delete id="deleteRole" parameterType="long">
delete from t_role where id = #{id}
</delete>
<select id="getRole" parameterType="long" resultType="cn.whc.April_11.pojo.Role">
select id, role_name as roleName, note from t_role where id = #{id}
</select>
<update id="updateRole" parameterType="cn.whc.April_11.pojo.Role">
update t_role
set role_name = #{roleName},
note = #{note}
where id = #{id}
</update>
</mapper>
5.其它配置(jdbc.properties、log4j.properties、pojo、测试类)
jdbc.properties
db.driverClassName=com.mysql.jdbc.Driver
db.url=jdbc:mysql://localhost/ssm?characterEncoding=utf-8
db.username=root
db.password=root
log4j.properties
# 配置根Logger
log4j.rootLogger=DEBUG, stdout
log4j.logger.org.mybatis=DEBUG
# 配置日志信息输出目的地Appender及Appender选项, 输出ConsoleAppender到控制台
log4j.appender.stdout=org.apache.log4j.ConsoleAppender
# 配置日志信息的格式(布局)及格式布局选项
log4j.appender.stdout.layout=org.apache.log4j.PatternLayout
log4j.appender.stdout.layout.ConversionPattern=%5p %d %C %m%n
pojo
public class Role {
private Long id;
private String roleName;
private String note;
/*****setter getter****/
测试类
public class AppTest {
@Test
public void test(){
ApplicationContext ctx = new ClassPathXmlApplicationContext("beans.xml");
RoleMapper roleMapper = ctx.getBean(RoleMapper.class);
Role role = new Role();
role.setRoleName("role_name_mapper");
role.setNote("note_mapper");
roleMapper.insertRole(role);
Long id = role.getId();
role = roleMapper.getRole(id);
role.setNote("note_mapper_update");
roleMapper.updateRole(role);
roleMapper.deleteRole(id);
}
}
6.配置pom.xml
mysql驱动包、spring相关的(beans、context、expression、core)、事务相关的(tx、orm、jdbc)、以及commons-logging、mybatis(mybatis和mybatis-spring整合包)
<properties>
<spring-version>4.0.0.RELEASE</spring-version>
</properties>
<groupId>cn.whc</groupId>
<artifactId>practice</artifactId>
<version>1.0-SNAPSHOT</version>
<packaging>war</packaging>
<dependencies>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.11</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>5.1.47</version>
</dependency>
<dependency>
<groupId>log4j</groupId>
<artifactId>log4j</artifactId>
<version>1.2.17</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-beans</artifactId>
<version>${spring-version}</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context</artifactId>
<version>${spring-version}</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-expression</artifactId>
<version>${spring-version}</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-core</artifactId>
<version>${spring-version}</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-tx</artifactId>
<version>${spring-version}</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-jdbc</artifactId>
<version>${spring-version}</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-orm</artifactId>
<version>${spring-version}</version>
</dependency>
<dependency>
<groupId>commons-logging</groupId>
<artifactId>commons-logging</artifactId>
<version>1.2</version>
</dependency>
<dependency>
<groupId>org.mybatis</groupId>
<artifactId>mybatis</artifactId>
<version>3.5.2</version>
</dependency>
<dependency>
<groupId>org.mybatis</groupId>
<artifactId>mybatis-spring</artifactId>
<version>1.3.2</version>
</dependency>
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>druid</artifactId>
<version>1.1.10</version>
</dependency>
</dependencies>
==============================================================================
TODO:写这些配置挺多的,找个时间学习下SpringBoot(配置太痛苦le)