本项目基于MyBatis3.2.5
1、新建Maven WebApp项目,pom.xml如下:
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>com.my.app</groupId>
<artifactId>MyBatis</artifactId>
<packaging>war</packaging>
<version>0.0.1-SNAPSHOT</version>
<name>MyBatis Maven Webapp</name>
<url>http://maven.apache.org</url>
<properties>
<org.slf4j-version>1.7.6</org.slf4j-version>
</properties>
<dependencies>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.11</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.mybatis</groupId>
<artifactId>mybatis</artifactId>
<version>3.2.5</version>
</dependency>
<!-- mysql -->
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>5.1.29</version>
</dependency>
<!-- Logging -->
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-api</artifactId>
<version>${org.slf4j-version}</version>
</dependency>
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>jcl-over-slf4j</artifactId>
<version>${org.slf4j-version}</version>
<scope>runtime</scope>
</dependency>
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-log4j12</artifactId>
<version>${org.slf4j-version}</version>
<scope>runtime</scope>
</dependency>
<dependency>
<groupId>log4j</groupId>
<artifactId>log4j</artifactId>
<version>1.2.17</version>
</dependency>
</dependencies>
<build>
<finalName>MyBatis</finalName>
</build>
</project>
2、User类
package com.my.model;
public class User {
private int id;
private String name;
private int age;
public User(){}
public User(String name, int age) {
this.name = name;
this.age = age;
}
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
}
3、UserDAO
package com.my.dao;
import org.apache.ibatis.annotations.Param;
import com.my.model.User;
public interface UserDAO {
public User selectOne(@Param("id")Integer id);
public void insertOne(User user);
public void deleteOne(@Param("id")Integer id);
public void updateOne(User user);
}
4、UserMapper.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.my.dao.UserDAO">
<select id="selectOne" parameterType="int" resultType="User">
select * from user where id = #{id,jdbcType=INTEGER}
</select>
<!-- <select id="getCustomer" parameterType="int" resultType="Customer"
> select * from customers where id= #{id} </select> -->
<insert id="insertOne" parameterType="User">
insert into user (name,age)
values(
#{name,jdbcType=VARCHAR},
#{age,jdbcType=INTEGER}
)
</insert>
<delete id="deleteOne" parameterType="java.lang.Integer">
delete from user
where id = #{id,jdbcType=INTEGER}
</delete>
<update id="updateOne" parameterType="User">
update user
set name=#{name,jdbcType=VARCHAR},
age=#{age,jdbcType=INTEGER}
where id= #{id,jdbcType=INTEGER}
</update>
</mapper>
5、mybatis-config.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>
<typeAliases>
<typeAlias alias="User" type="com.my.model.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:3306/MyBatisDB?useUnicode=true&characterEncoding=gbk"/>
<property name="username" value="root"/>
<property name="password" value=""/>
</dataSource>
</environment>
</environments>
<mappers>
<mapper resource="com/my/mapper/UserMapper.xml"/>
</mappers>
</configuration>
6、UserDAOTest
package com.my.dao.test;
import static org.junit.Assert.*;
import java.io.Reader;
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.Assert;
import org.junit.Before;
import org.junit.BeforeClass;
import org.junit.Test;
import com.my.dao.UserDAO;
import com.my.model.User;
//2014-7-10
public class UserDAOTest {
private static SqlSessionFactory sqlSessionFactory;
//SqlSession 请求级别的,每次请求新建一个,不要配置成静态的,每次用完必须关闭,
//insert update delete 必须开启事务,并且commit才能保存到数据库里
//static SqlSession session = null;
UserDAO userDao = null;
@BeforeClass
public static void setUpBeforeClass() throws Exception {
try {
Reader reader = Resources.getResourceAsReader("mybatis-config.xml");
sqlSessionFactory = new SqlSessionFactoryBuilder().build(reader);
} catch (Exception e) {
e.printStackTrace();
}
}
@Before
public void setUp() throws Exception {
}
public void testSelectOne() {
// fail("Not yet implemented");
User user = userDao.selectOne(1);
if (user != null) {
System.out.println("Name=" + user.getName());
}
Assert.assertNotNull(user);
}
@Test
public void testInsertOne() {
SqlSession session = sqlSessionFactory.openSession();
userDao = session.getMapper(UserDAO.class);
User user = new User();
user.setAge(7);
user.setName("test77");
userDao.insertOne(user);
session.commit();
session.close();
Assert.assertTrue(true);
}
@Test
public void testDeleteOne() {
SqlSession session = sqlSessionFactory.openSession();
userDao = session.getMapper(UserDAO.class);
userDao.deleteOne(11);
session.commit();
session.close();
Assert.assertTrue(true);
}
@Test
public void testUpdateOne() {
SqlSession session = sqlSessionFactory.openSession();
userDao = session.getMapper(UserDAO.class);
User user = new User();
user.setId(11);
user.setAge(7);
user.setName("name77");
userDao.updateOne(user);
session.commit();
session.close();
Assert.assertTrue(true);
}
}
7、执行单元测试 运行ok
demo下载地址:http://download.csdn.net/detail/yong230/7659425
原文:http://blog.csdn.net/yong230/article/details/38017371