<?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>
<settings>
<setting name="defaultStatementTimeout" value="1000"/>
</settings>
<typeAliases>
<typeAlias alias="user" type="com.pt.User"/>
</typeAliases>
<environments default="development">
<environment id="development">
<transactionManager type="JDBC" />
<!-- 配置数据库连接信息 -->
<dataSource type="POOLED">
<property name="driver" value="com.mysql.jdbc.Driver" />
<property name="url" value="jdbc:mysql://localhost:8066/TESTDB" />
<property name="username" value="test" />
<property name="password" value="test" />
</dataSource>
</environment>
</environments>
<mappers>
<mapper resource="com/pt/map/UserMap.xml"/>
</mappers>
</configuration>
conf.xml
package com.pt; public class User {
public int userId;
public String userName;
public int getUserId() {
return userId;
}
public void setUserId(int userId) {
this.userId = userId;
}
public String getUserName() {
return userName;
}
public void setUserName(String userName) {
this.userName = userName;
} }
User.java
<?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.pt.map.UserMap">
<resultMap type="user" id="userMap">
<!--property值 是类的成员名,column 是数据库字段名 -->
<result property="userId" column="id" />
<result property="userName" column="name" />
</resultMap>
<select id="getUser" parameterType="int" resultMap="userMap">
select * from test where id=#{id}
</select> <insert id="addUser" parameterType="user" timeout="1">
insert into test(id,name) values(#{userId},#{userName})
</insert>
</mapper>
UserMap.xml
package com.pt; import java.io.Reader;
import java.util.Date; 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 org.junit.AfterClass;
import org.junit.BeforeClass;
import org.junit.Test; public class TestUser {
//mybatis的配置文件
static String resource = "conf.xml";
//使用MyBatis提供的Resources类加载mybatis的配置文件(它也加载关联的映射文件)
static Reader reader = null;
//构建sqlSession的工厂
static SqlSessionFactory sessionFactory = null;
//创建能执行映射文件中sql的sqlSession
static SqlSession session = null;
@BeforeClass
public static void setUp()throws Exception{
reader = Resources.getResourceAsReader(resource);
//构建sqlSession的工厂
sessionFactory = new SqlSessionFactoryBuilder().build(reader);
//创建能执行映射文件中sql的sqlSession
session = sessionFactory.openSession();
System.out.println("开始测试之前~~~~~~~~~"+ new Date());
} @AfterClass
public static void tearDown(){
System.out.println("开始完成之后~~~~~~~~~"+ new Date());
} @Test
public void testGet() throws Exception{ /**
* 映射sql的标识字符串,
* me.gacl.mapping.userMapper是userMapper.xml文件中mapper标签的namespace属性的值,
* getUser是select标签的id属性值,通过select标签的id属性值就可以找到要执行的SQL
*/
String statement = "com.pt.map.UserMap.getUser";//映射sql的标识字符串
//执行查询返回一个唯一user对象的sql
int id = 1630002;
User user = session.selectOne(statement, id);
System.out.println(user.getUserId());
} @Test
public void testAdd() throws Exception{
String statement = "com.pt.map.UserMap.addUser";//映射sql的标识字符串
//执行查询返回一个唯一user对象的sql
User newUser = new User();
int id = 1005000;
int insert_result = -88;
while(id++ != 1009000){
try{
newUser.setUserId(id);
insert_result = session.insert(statement, newUser);
session.commit();
System.out.println(id + "===INSERT SUCCESS===" + insert_result);
}catch(Exception e){
System.out.println(id + "===INSERT FAIL===" + insert_result);
e.printStackTrace();
}
Thread.sleep(0, 10);
}
} }
test