Mabatis应用
MyBatis使用结构
java目录下:com.example.dao.TestMapper.java接口
public interface TestMapper{
Pojo selectPojoById(Integer id);
}
resources目录下:com.example.dao.TestMapper.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.example.dao.TestMapper">
<select id="selectPojoById" resultType="student" parameterType="java.lang.Integer">
select * from pojo where id = #{id}
</select>
</mapper>
MyBatis独立使用
全局配置文件MyBatisConfig.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>
<!--读取jdbc.properties-->
<properties resource="jdbc.properties"/>
<settings>
<!--配置log4j日志-->
<setting name="logImpl" value="log4j"/>
</settings>
<typeAliases>
<!--实体类注册别名-->
<package name="com.example.pojo"/>
</typeAliases>
<environments default="mysql">
<environment id="mysql">
<transactionManager type="JDBC"/>
<dataSource type="POOLED">
<property name="driver" value="${driver}"/>
<property name="url" value="${url}"/>
<property name="username" value="${username}"/>
<property name="password" value="${password}"/>
</dataSource>
</environment>
</environments>
<mappers>
<!--扫描Mapper.xml文件-->
<package name="com.example.dao">
</mappers>
</configuration>
java目录下:com.example.dao.impl.TestMapperImpl.java实现类
public class TestMapperImpl implements TestMapper{
private SqlSession sqlSession;
@Override
public Pojo selectPojoById(Integer id){
Pojo pojo;
sqlSession = this.open();
pojo = sqlSession.selectOne("TestMapper.selectPojoById",id);
this.close();
return pojo;
}
//SqlSession开启方法
private SqlSession open(){
try {
InputStream resourceAsStream = Resources.getResourceAsStream("MyBatisConfig.xml");
SqlSessionFactory sqlSessionFactory= new SqlSessionFactoryBuilder().build(resourceAsStream);
sqlSession = sqlSessionFactory.openSession(true);
resourceAsStream.close();
} catch (IOException e) {
e.printStackTrace();
}
return sqlSession;
}
//SqlSession关闭方法
private void close(){
if (sqlSession != null){
sqlSession.close();
}
}
}
Spring集成MyBatis
Spring配置文件applicationContext.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">
<!--读取jdbc.properties-->
<context:property-placeholder location="classpath:jdbc.properties"/>
<!--扫描包里除被@Controller标记的类-->
<context:component-scan base-package="com.example">
<context:exclude-filter type="annotation" expression="org.springframework.stereotype.Controller"/>
</context:component-scan>
<!--配置alibaba druid数据源-->
<bean id="dataSource" class="com.alibaba.druid.pool.DruidDataSource">
<property name="driverClassName" value="${jdbc.driver}"/>
<property name="url" value="${jdbc.url}"/>
<property name="username" value="${jdbc.username}"/>
<property name="password" value="${jdbc.password}"/>
</bean>
<!--配置dao层接口与xml的映射-->
<bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
<property name="basePackage" value="com.example.dao"/>
</bean>
<!--配置mybaties的SqlSession工厂方法的映射-->
<bean class="org.mybatis.spring.SqlSessionFactoryBean">
<property name="dataSource" ref="dataSource"/>
<property name="typeAliasesPackage" value="com.example.pojo"/>
</bean>