Mybatis独立使用
1.建立PO
public class User {
private Integer id;
private String name;
private Integer age;
public User(String name, Integer age) {
this.age = age;
this.name = name;
}
public User() {
super();
}
注意:对象一定要有无参构造方法,不然在查询数据库时,无法通过反射构造出实例对象。这里我猜测用的newInstance方法创建的对象。
2.建立Mapper
数据库操作的映射文件。可以通过配置文件制定方法对应的SQL,或者直接注解方式进行SQL制定。
public interface UserMapper {
public void insertUser(User user);
public User getUser(Integer id);
}
3.建立配置文件
配置文件主要用于配置程序中可变性高的设置,MyBatis中的配置文件主要封装在configuration中。
configuration:根元素
properties:定义配置外在化。
settings:一些全局性的配置。
typeAliases:为一些类定义别名。
typeHandlers:定义类型处理,也就是定义Java类型与数据库中的数据类型之间的转换关系。
objectFactory:用于指定结果集对象的实例是如何创建的。
plugins:mybatis插件,可以修改mybatis内部的允许规则。
environments:环境。
environment:配置mybatis的环境。
environment包含tansactionManager事务管理器和dataSource数据源。
mappers:指定映射文件或映射类。
<?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>
<!--环境配置,连接的数据库,这里使用的是MySQL-->
<environments default="mysql">
<environment id="mysql">
<!--指定事务管理的类型,这里简单使用Java的JDBC的提交和回滚设置-->
<transactionManager type="JDBC"></transactionManager>
<!--dataSource 指连接源配置,POOLED是JDBC连接对象的数据源连接池的实现-->
<dataSource type="POOLED">
<property name="driver" value="com.mysql.jdbc.Driver"></property>
<property name="url" value="jdbc:mysql://127.0.0.1:3306/mybbs"></property>
<property name="username" value="root"></property>
<property name="password" value="root"></property>
</dataSource>
</environment>
</environments>
<mappers>
<!--这是告诉Mybatis区哪找持久化类的映射文件,对于在src下的文件直接写文件名,
如果在某包下,则要写明路径,如:com/mybatistest/config/User.xml-->
<mapper resource="resource/UserMapper.xml"></mapper>
</mappers>
</configuration>
4.建立映射文件
对应于Mybatis全局配置中的mappers配置属性,用于建立对应数据库操作接口的SQL映射。Mybatis会将这里定义的SQL于对应的Java接口相关联,保证在Mybatis中调用接口的时候回到数据库中执行相应的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">
<!-- namespace必须是UserMapper接口的路径 -->
<mapper namespace="Mapper.UserMapper">
<!-- id必须和UserMapper接口中的接口方法名相同 -->
<insert id="insertUser" parameterType="User">
<!-- sql ,结尾不能有分号-->
</insert>
<select id="getUser" parameterType="java.lang.Integer" resultType="User">
select *from user where id=#{id}
</select>
</mapper>
Spring整合Mybatis
1.spring配置
<bean id="dataSource" class="org.apache.commons.dbcp.BasicDatasource">
</bean>
<bean id="sqlSessionFactory" class="org.mybatis.Spring.SqlSessionFactoryBean">
<property name="configLocation" value="classpath:MyBatis-Configruation.xml" />
<property name="dataSource" ref="dataSource" />
</bean>
<bean id="userMapper" class="org.mybatis.Spring.mapper.MapperFactoryBean">
<property name="mapperInterface" value="xxx.xxx.UserMapper" />
<property name="sqlSessionFactory" ref="sqlSessionFactory" />
</bean>
2.Mybatis配置文件
<configuration>
<typeAliases>
<typeAlias alias="User" type="xxx.xxx.User"/>
</typeAliases>
<mappers>
<mapper resource="xxx/xxx/UserMapper.xml"/>
</mappers>
</configuration>
3.sql映射文件不变
看看SqlSessionFactoryBean
InitializingBean:在bean初始化的时候会调用afterProperties方法。
FactoryBean:调用getBean方法获取bean时,获取的是getObject返回的实例。
最后通过targetConfiguration,bulid出SqlSessionFactory对象。
获取sqlSessionFactory
注意到除了要配置SqlSessionFactoryBean,还需要配置MapperFactoryBean。
MapperFactoryBean是一个factorybean,通过getObject对象获取mapper对象。
映射接口的验证:sqlSession会根据接口动态创建相应的代理类,所以接口必不可少。
映射文件存在性的验证:Mybatis并没有手动调用configuration.addMapper方法,而是在映射文件读取过程中一旦解析到如<mapper namespace="Mapper.UserMapper">
,便会自动进行类型映射的注册。
然后看看MapperScannerConfigurer类
这个类可以扫描特定的包,自动帮我们创建映射器。
<!-- 删除
<bean id="userMapper" class="org.mybatis.Spring.mapper.MapperFactoryBean">
<property name="mapperInterface" value="xxx.xxx.UserMapper" />
<property name="sqlSessionFactory" ref="sqlSessionFactory" />
</bean>
-- >
<bean class=org.mybatis.Spring.mapper.MapperScannerConfigurer">
<property name="basePackage" value="test.mybatis.dao"/>
</bean>
看下这个类的层次结构
看看里头的方法
该类在postProcessBeanDefinitionRegistry方法中调用扫描包路径,注册mapper。
processPropertyPlaceHolders方法是干属性处理的。只用在processPropertyPlaceHolders为true时才被调用。
解决的问题是,使用占位符问题,在使用属性的时候还并未将属性文件中的属性加载至内存,Spring还无法使用它。
这样mapperScannerConfigurer在扫描的过程中自动帮我们注册号