http://www.cnblogs.com/xdp-gacl/p/4262895.html

继续参考这篇文章写Mybaits学习总结

上一章,我修改了编码,统一为UTF8之后,便没有编码错误

<?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,namespace的值习惯上设置成包名+sql映射文件名,这样就能够保证namespace的值是唯一的
例如namespace="me.gacl.mapping.userMapper"就是me.gacl.mapping(包名)+userMapper(userMapper.xml文件去除后缀)
-->
<mapper namespace="pw.jonwinters.mybaits.userMapper">
<!-- 在select标签中编写查询的SQL语句, 设置select标签的id属性为getUser,id属性值必须是唯一的,不能够重复
使用parameterType属性指明查询时使用的参数类型,resultType属性指明查询返回的结果集类型
resultType="me.gacl.domain.User"就表示将查询结果封装成一个User类的对象返回
User类就是users表所对应的实体类
-->
<!--
根据id查询得到一个user对象
-->
<select id="getUser" parameterType="int"
resultType="pw.jonwinters.mybaits.User">
select * from users where id=#{id}
</select> <select id="getAllUsers" resultType="pw.jonwinters.mybaits.User">
select * from users
</select> <!-- 创建用户(Create) -->
<insert id="addUser" parameterType="pw.jonwinters.mybaits.User">
insert into users(name,age) values(#{name},#{age})
</insert> <update id="updateUser" parameterType="pw.jonwinters.mybaits.User">
update users set name = #{name},age = #{age} where id = #{id}
</update> <select id="getAgeThanUsers" parameterType="int" resultType="pw.jonwinters.mybaits.User">
select * from users where age > #{0}
</select> <select id="getAgeThanUsers_2" parameterType="pw.jonwinters.mybaits.User" resultType="pw.jonwinters.mybaits.User">
select * from users where age > #{age} and id > 7
</select> <delete id="delUserByID" parameterType="int" >
delete from users where id = #{0}
</delete> <delete id="delUserByID_2" parameterType="pw.jonwinters.mybaits.User" >
delete from users where id = #{id}
</delete>
</mapper>
<delete id="delUserByID" parameterType="int" >
delete from users where id = #{0}
</delete>
//这里的#{0}我测试过了,无论里面填写什么都会被解析成    session.delete(str,xxx) 第二个参数xxxx
package pw.jonwinters.mybaits;

import java.util.List;
import java.io.InputStream; import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;
import org.apache.ibatis.session.SqlSessionFactoryBuilder;
import org.junit.Before;
import org.junit.Test; import static org.junit.Assert.*; public class MybaitsTest { private SqlSession session ; @Before
public void before(){
String res = "mybaitsConf.xml";
InputStream is = MybaitsTest.class.getClassLoader().getResourceAsStream(res);//将文件变成输入流
assertNotNull(is);
SqlSessionFactory sessionFactory = new SqlSessionFactoryBuilder().build(is);//工厂建立配置文件对象
assertNotNull(sessionFactory);
session = sessionFactory.openSession(true);//打开事务 true为自动提交 }
@Test
public void sqlGetByID(){
String str = "pw.jonwinters.mybaits.userMapper.getUser";
User user = session.selectOne(str,3);
assertNotNull(user);
System.out.println(user);
System.out.println("-----------sqlGetByID");
} @Test
public void sqlTestGetAll(){
String str = "pw.jonwinters.mybaits.userMapper.getAllUsers";
List<User> users = session.selectList(str);
for(User u : users){
System.out.println(u);
}
System.out.println("-----------sqlTestGetAll");
} //@Test
public void sqlInsertUser(){ User user = new User();
//user.setId(13);
user.setAge(16);
user.setName("赵七");
assertEquals(session.insert("pw.jonwinters.mybaits.userMapper.addUser", user), 1);
//session.insert("pw.jonwinters.mybaits.userMapper.addUser", user);
session.commit();
session.close();
} @Test
public void sqlAgeThanUsers(){
String str = "pw.jonwinters.mybaits.userMapper.getAgeThanUsers";
List<User> users = session.selectList(str, 14);
for(User u : users){
System.out.println(u);
}
System.out.println("-----------getAgeThanUsers");
} @Test
public void sqlAgeThanUsers_2(){
String str = "pw.jonwinters.mybaits.userMapper.getAgeThanUsers_2";
User user = new User();
user.setAge(14);
List<User> users = session.selectList(str,user );
for(User u : users){
System.out.println(u);
}
System.out.println("-----------getAgeThanUsers_2");
} @Test
public void sqlUpateUserByID(){
String str = "pw.jonwinters.mybaits.userMapper.updateUser";
User user = new User();
user.setAge(221);
user.setName("赵288");
user.setId(8);
assertEquals(1, session.update(str, user));
System.out.println("-----------sqlUpateUserByID");
} //@Test
public void sqlDelByID(){
String str = "pw.jonwinters.mybaits.userMapper.delUserByID";
assertEquals(1,session.delete(str, 3));
System.out.println("-----------sqlDelByID");
} @Test
public void sqlDelByID_2(){
String str = "pw.jonwinters.mybaits.userMapper.delUserByID_2";
User user = new User();
user.setId(8);
assertEquals(1,session.delete(str,user));
System.out.println("-----------sqlDelByID_2");
}
}

我自己稍作了一些Sql的修改,注解查询的方法目前没有验证

05-04 05:06