本文主要是介绍myBatis配置和测试,对大家解决编程问题具有一定的参考价值,需要的程序猿们随着小编来一起学习吧!
测试
package dao;
import org.apache.ibatis.session.SqlSession;
import org.junit.Test;
import pojo.User;
import utils.myBatisUtils;
import java.util.List;
public class daotest {
@Test
public void test(){
//第一步:获得sqlSession对象
SqlSession sqlSession = myBatisUtils.getSqlSession();
//方式一:getMapper
UserDao mapper = sqlSession.getMapper(UserDao.class);
List<User> userList = mapper.getUserList();
for (User user : userList) {
System.out.println(user);
}
//关闭sqlSession
sqlSession.close();
}
}
做一个接口
package dao;
import pojo.User;
import java.util.List;
public interface UserDao {
List<User> getUserList();
}
工具类
package utils;
import org.apache.ibatis.io.Resources;
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;
import org.apache.ibatis.session.SqlSessionFactoryBuilder;
import java.io.IOException;
import java.io.InputStream;
/**
* @author liu
*/
public class myBatisUtils {
private static SqlSessionFactory sqlSessionFactory =null;
static {
//mybatis第一步,获取sqlSessionFactory对象
try {
String resource = "mybatis-config.xml";
InputStream ips = Resources.getResourceAsStream(resource);
sqlSessionFactory = new SqlSessionFactoryBuilder().build(ips);
} catch (IOException e) {
e.printStackTrace();
}
}
//既然有了sqlSessionFactory,顾名思义,可以创造SqlSession的实例
//SqlSession完全包含了面向数据库的所有sql命令和方法
public static SqlSession getSqlSession(){
return sqlSessionFactory.openSession();
}
}
mybatis-3-config.xml
<?xml version="1.0" encoding="UTF8" ?>
<!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="com.mysql.cj.jdbc.Driver"/>
<property name="url" value="jdbc:mysql://localhost:3306/mybatis?useUnicode=ture&useSSL=true&characterEncoding=UTF-8"/>
<property name="username" value="root"/>
<property name="password" value="123456"/>
</dataSource>
</environment>
</environments>
<!--每一个Mappper.XML都需要在MyBatis核心配置文件中注册-->
<mappers>
<mapper resource="UserDao.xml"/>
</mappers>
</configuration>
userDao.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="dao.UserDao">
<select id="getUserList" resultType="pojo.User">
select * from myBatis.user where id="1";
</select>
</mapper>
maven会过滤,需要加上配置
<!--在build中配置resources,来防止我们资源导出失败的问题-->
<!-- <build>
<resources>
<resource>
<directory>src/main/resources</directory>
<includes>
<include>**/*.properties</include>
<include>**/*.xml</include>
</includes>
<filtering>true</filtering>
</resource>
<resource>
<directory>src/main/java</directory>
<includes>
<include>**/*.properties</include>
<include>**/*.xml</include>
</includes>
<filtering>true</filtering>
</resource>
</resources>
</build>-->
这篇关于myBatis配置和测试的文章就介绍到这儿,希望我们推荐的文章对大家有所帮助,也希望大家多多支持为之网!