mybatis学习日记1

一个实体类:

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");
	}


类放置位置如图:




评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值