一个实体类:
public class Author {
private String id;
private String name;
private String password;
private String email;
...
}
数据库建表:
create table t_author(author_id varchar(40) primary key,"
+ "author_username varchar(30),"
+ "author_password varchar(20), author_email varchar(50));
autho-mapper.xml配置(插入测试):
<mapper namespace="com.deppon.mybatis.domain">
<insert id="insertAuthor" parameterType="com.deppon.mybatis.domain.Author">
insert into t_author
(author_id,author_username,author_password,author_email)
values
(#{id},#{name},#{password},#{email})
</insert>
...
</mapper>
测试类(插入测试):
...
@Test
public void testInsert() throws SQLException {
Author au = new Author("3324", "豆豆", "456789", "386@dd.com");
sqlSession.insert("com.deppon.mybatis.domain.insertAuthor", au);
}
...
author-mapper.xml配置(查询):
<mapper namespace="com.deppon.mybatis.domain">
<select id="selectAuthorById" parameterType="String"
resultType="com.deppon.mybatis.domain.Author">
select author_id as id,
author_username as name,
author_password as password,
author_email as email
from t_author where
author_id = #{id}
</select>
<resultMap type="com.deppon.mybatis.domain.Author" id="authorResult">
<id column="author_id" property="id" />
<result column="author_username" property="name" />
<result column="author_password" property="password" />
<result column="author_email" property="email" />
</resultMap>
<select id="selectAuthor" resultMap="authorResult">
select
author_id as id,
author_username as name,
author_password as password,
author_email as email
from t_author
</select>
</mapper>
测试类(查询):
@Test
public void testSelectOne() {
Author a = new Author();
a = (Author) sqlSession.selectOne(
"com.deppon.mybatis.domain.selectAuthorById", "386");
System.out.println(a.getName() + " 的邮箱是: " + a.getEmail());
}
@Test
public void testSelect() {
List<Author> list = new ArrayList<Author>();
list = sqlSession.selectList("com.deppon.mybatis.domain.selectAuthor");
for (Author au : list) {
System.out.println(au.toString());
}
}
author-mapper.xml配置(更新update):
<mapper namespace="com.deppon.mybatis.domain">
<update id="updateAuthor" parameterType="com.deppon.mybatis.domain.Author">
update t_author set author_email = #{email} where author_id=#{id}
</update>
</mapper>
测试类(更新update):
@Test
public void testUpdate() {
Author au = new Author();
au.setId("32");
au.setEmail("xiugaixiugaixiugai@xiugai.com");
sqlSession.update("com.deppon.mybatis.domain.updateAuthor", au);
}
author-mapper.xml配置(删除):
<mapper namespace="com.deppon.mybatis.domain">
<delete id="deleteAuthor" parameterType="String">
delete from t_author where author_id=#{id}
</delete>
</mapper>测试类(删除):
@Test
public void testDelete() {
Author au = new Author();
au.setId("333");
sqlSession.delete("com.deppon.mybatis.domain.daleteAuthor", au);
}
ps:在测试的时候,我加了这两个方法,就不用每次都去手动的去加载配置文件。
@Before
public void init() throws IOException {
Reader reader = Resources
.getResourceAsReader("mybatis.xml");
SqlSessionFactoryBuilder builder = new SqlSessionFactoryBuilder();
SqlSessionFactory factory = builder.build(reader);
sqlSession = factory.openSession(true);
}
@After
public void finalized() throws SQLException {
sqlSession.getConnection().createStatement().execute("SHUTDOWN");
}
类放置位置如图:

194

被折叠的 条评论
为什么被折叠?



