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>
<environments default="development">
<environment id="development">
<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 resource="org/mybatis/example/BlogMapper.xml"/>
</mappers>
</configuration>
获取SqlSession对象
public class MybatisUtil {
private static SqlSessionFactory sqlSessionFactory ;
static{
try {
//MyBatis核心配置文件路径
String resource = "mybatis-config.xml";
//文件流
InputStream inputStream = Resources.getResourceAsStream(resource);
//获取sqlSessionFactory对象
sqlSessionFactory = new SqlSessionFactoryBuilder().build(inputStream);
} catch (IOException e) {
e.printStackTrace();
}
}
//通过sqlSessionFactory对象获取SqlSession
public static SqlSession getSqlSession(){
return sqlSessionFactory.openSession();
}
}
映射的 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">
<mapper namespace="org.mybatis.example.BlogMapper">
<select id="selectBlog" resultType="Blog">
select * from Blog where id = #{id}
</select>
</mapper>
测试案例
public class UserDaoTest {
@Test
public void test(){
//第一步获取sqlSession对象
SqlSession sqlSession = MybatisUtil.getSqlSession();
try {
//执行sql
UserMapper mapper = sqlSession.getMapper(UserMapper.class);
//获取结果
List<User> userList = mapper.getUserList();
//遍历结果
for (User user : userList) {
System.out.println(user);
}
}catch (Exception e){
e.printStackTrace();
}finally {
//关闭sqlSession
sqlSession.close();
}
}
@Test
public void getUserById(){
//第一步获取sqlSession对象
SqlSession sqlSession = MybatisUtil.getSqlSession();
try {
//执行sql
UserMapper mapper = sqlSession.getMapper(UserMapper.class);
//获取结果
User userById = mapper.getUserById(1);
//输出结果
System.out.println(userById);
}catch (Exception e){
e.printStackTrace();
}finally {
//关闭sqlSession
sqlSession.close();
}
}
@Test
public void addUser(){
//第一步获取sqlSession对象
SqlSession sqlSession = MybatisUtil.getSqlSession();
try {
//执行sql
UserMapper mapper = sqlSession.getMapper(UserMapper.class);
//获取结果
int conut = mapper.addUser(new User(4, "牛牛", "1212"));
if(conut >0)System.out.println("添加成功");
//提交事务 ,这里必须手动提交事务,不然添加不成功 ,增删改都需要提交事务
sqlSession.commit();
}catch (Exception e){
e.printStackTrace();
}finally {
//关闭sqlSession
sqlSession.close();
}
}
@Test
public void updateUser(){
//第一步获取sqlSession对象
SqlSession sqlSession = MybatisUtil.getSqlSession();
try {
//执行sql
UserMapper mapper = sqlSession.getMapper(UserMapper.class);
//获取结果
int conut = mapper.updateUser(new User(4, "龙龙", "9999"));
if(conut >0)System.out.println("修改OK");
//提交事务 ,这里必须手动提交事务,不然添加不成功
sqlSession.commit();
}catch (Exception e){
e.printStackTrace();
}finally {
//关闭sqlSession
sqlSession.close();
}
}
@Test
public void deleteUser(){
//第一步获取sqlSession对象
SqlSession sqlSession = MybatisUtil.getSqlSession();
try {
//执行sql
UserMapper mapper = sqlSession.getMapper(UserMapper.class);
//获取结果
int conut = mapper.deleteUser(4);
if(conut >0)System.out.println("删除成功");
//提交事务 ,这里必须手动提交事务,不然添加不成功
sqlSession.commit();
}catch (Exception e){
e.printStackTrace();
}finally {
//关闭sqlSession
sqlSession.close();
}
}
}