jdbc.driver=com.mysql.jdbc.Driver
jdbc.url=jdbc:mysql://localhost:13306/mybatis
jdbc.username=root
jdbc.password=root
DOCTYPE configuration
PUBLIC "-//mybatis.org//DTD Config 3.0//EN"
"http://mybatis.org/dtd/mybatis-3-config.dtd">
<configuration>
<properties resource="jdbc.properties">properties>
<typeAliases >
<package name="com.sdjzu.mybatis.pojo"/>
typeAliases>
<environments default="development">
<environment id="development">
<transactionManager type="JDBC"/>
<dataSource type="POOLED">
<property name="driver" value="${jdbc.driver}"/>
<property name="url" value="${jdbc.url}"/>
<property name="username" value="${jdbc.username}"/>
<property name="password" value="${jdbc.password}"/>
dataSource>
environment>
environments>
<mappers>
<package name="com.sdjzu.mybatis.mapper"/>
mappers>
configuration>
DOCTYPE mapper
PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
"http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<mapper namespace="">
mapper>
package com.sdjzu.mybatis.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;
public class SqlSessionUtils {
public static SqlSession getSqlSession(){
SqlSession sqlSession=null;
try {
InputStream is = Resources.getResourceAsStream("mybatis-config.xml");
SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(is);
sqlSession = sqlSessionFactory.openSession(true);
} catch (IOException e) {
e.printStackTrace();
}
return sqlSession;
}
}
测试
package com.sdjzu.mybatis.test;
import com.sdjzu.mybatis.mapper.ParameterMapper;
import com.sdjzu.mybatis.pojo.User;
import com.sdjzu.mybatis.utils.SqlSessionUtils;
import org.apache.ibatis.session.SqlSession;
import org.junit.Test;
import java.util.List;
public class ParameterMapperTest {
@Test
public void testGetAllUser(){
SqlSession sqlSession = SqlSessionUtils.getSqlSession();
ParameterMapper mapper = sqlSession.getMapper(ParameterMapper.class);
List<User> allUser = mapper.getAllUser();
/*用强for循环*/
for (User user:allUser){
System.out.println(user);
}
}
}
测试结果
DEBUG 04-03 23:03:17,282 ==> Preparing: select * from t_user (BaseJdbcLogger.java:137)
DEBUG 04-03 23:03:17,313 ==> Parameters: (BaseJdbcLogger.java:137)
DEBUG 04-03 23:03:17,313 <== Total: 8 (BaseJdbcLogger.java:137)
User{id=1, username='jay', password='123456', age=23, sex='?', email='[email protected]'}
User{id=3, username='admin', password='123456', age=23, sex='?', email='[email protected]'}
User{id=4, username='admin', password='123456', age=23, sex='?', email='[email protected]'}
User{id=5, username='admin', password='123456', age=23, sex='?', email='[email protected]'}
User{id=6, username='admin', password='123456', age=23, sex='?', email='[email protected]'}
User{id=7, username='admin', password='123', age=23, sex='?', email='[email protected]'}
User{id=8, username='admin', password='123456', age=23, sex='?', email='[email protected]'}
User{id=9, username='admin', password='123456', age=23, sex='?', email='[email protected]'}