码迷,mamicode.com
首页 > 其他好文 > 详细

mybatis实践

时间:2015-05-08 01:54:00      阅读:175      评论:0      收藏:0      [点我收藏+]

标签:

看mybatis的文档感觉很简单,总是眼高手低,真正写起代码,错误百出,看来以后还得多实际写代码来学习

目录结构

技术分享

文件:
jdbc.properties

driver=com.mysql.jdbc.Driver
url=jdbc:mysql://localhost:3306/database?useUnicode=true&characterEncoding=utf-8
username=root
password=yourpassword



mybatis.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>
	<properties resource="jdbc.properties" />
	
	<!-- 
		这里指定的别名,是为了简写mapper.xml中的resultType中的名称
		<mapper>标签中的namespace不能使用些简写
	 -->
	<typeAliases>
		<typeAlias alias="Card" type="cn.creval.study.mybatis.entity.Card"/>
	</typeAliases>
	
	<environments default="development">
		<environment id="development">
			<transactionManager type="JDBC" />
			<dataSource type="POOLED">
				<property name="driver" value="${driver}"/>
				<property name="url" value="${url}"/>
				<property name="username" value="${username}"/>
				<property name="password" value="${password}"/>
			</dataSource>
		</environment>
	</environments>
	
	<mappers>
		<mapper resource="cn/creval/study/mybatis/entity/card.xml"/>
	</mappers>
</configuration>



card.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="cn.creval.study.mybatis.entity.Card">
	<sql id="columns">
		id
		,shop_id as shopId
		,code
		,name
		,consumption_types as consumptionTypes
		,discount
		,create_date as createDate
		,is_delete as isDelete
	</sql>

	<select id="selectCard" parameterType="java.lang.String" resultType="Card">
		select
		<include refid="columns"></include>
		from card where id = #{id}
	</select>
</mapper>



mybatis-mapper.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>
	<properties resource="jdbc.properties" />
	
	<!-- 
		这里指定的别名,是为了简写mapper.xml中的resultType中的名称
		<mapper>标签中的namespace不能使用些简写
	 -->
	<typeAliases>
		<typeAlias alias="Card" type="cn.creval.study.mybatis.entity.Card"/>
	</typeAliases>
	
	<environments default="development">
		<environment id="development">
			<transactionManager type="JDBC" />
			<dataSource type="POOLED">
				<property name="driver" value="${driver}"/>
				<property name="url" value="${url}"/>
				<property name="username" value="${username}"/>
				<property name="password" value="${password}"/>
			</dataSource>
		</environment>
	</environments>
	
	<mappers>
		<mapper resource="cn/creval/study/mybatis/entity/card-mapper.xml"/>
	</mappers>
</configuration>



card-mapper.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="cn.creval.study.mybatis.dao.CardMapper">
	<sql id="columns">
		id
		,shop_id as shopId
		,code
		,name
		,consumption_types as consumptionTypes
		,discount
		,create_date as createDate
		,is_delete as isDelete
	</sql>

	<select id="selectCard" parameterType="java.lang.String" resultType="Card">
		select
		<include refid="columns"></include>
		from card where id = #{id}
	</select>
</mapper>



CardMapper.java
package cn.creval.study.mybatis.dao;

import cn.creval.study.mybatis.entity.Card;

public interface CardMapper {
	Card selectCard(String id);
}



MybatisTest.java
package test;

import java.io.IOException;
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.After;
import org.junit.Assert;
import org.junit.Test;

import cn.creval.study.mybatis.dao.CardMapper;
import cn.creval.study.mybatis.entity.Card;

public class MbatisTest {
	
	public SqlSession session;
	public final String CARD_NAMESPACE = "cn.creval.study.mybatis.entity.Card";
	
	public void init() throws IOException {
		String resource = "mybatis.xml";
		Reader reader = Resources.getResourceAsReader(resource);
		SqlSessionFactoryBuilder builder = new SqlSessionFactoryBuilder();
		SqlSessionFactory sessionFactory = builder.build(reader);
		session = sessionFactory.openSession();
		Assert.assertNotNull(session);
		System.out.println("创建会话成功!");
	}
	
	/**
	 * 使用Mapper
	 * @throws IOException
	 */
	public void initMapper() throws IOException {
		String resource = "mybatis-mapper.xml";
		Reader reader = Resources.getResourceAsReader(resource);
		SqlSessionFactoryBuilder builder = new SqlSessionFactoryBuilder();
		SqlSessionFactory sessionFactory = builder.build(reader);
		session = sessionFactory.openSession();
		Assert.assertNotNull(session);
		System.out.println("创建会话成功!");
	}
	
	@After
	public void close() {
		if (session != null) {
			session.close();
			session = null;
		}
		
		Assert.assertNull(session);
		System.out.println("会话关闭成功!");
	}
	
	@Test
	public void test1() throws IOException {
		init();
		
		String id = "20899f25f50e4a8d89a05d094b9d5795";
		Card card = session.selectOne(CARD_NAMESPACE + ".selectCard", id);
		Assert.assertEquals(id, card.getId());
		System.out.println(card);
	}
	
	@Test
	public void testMapper() throws IOException {
		initMapper();
		//通过session获取Mapper
		CardMapper mapper = session.getMapper(CardMapper.class);
		String id = "20899f25f50e4a8d89a05d094b9d5795";
		Card card = mapper.selectCard(id);
		System.out.println(card);
	}
}




mybatis实践

标签:

原文地址:http://my.oschina.net/lhplj/blog/412055

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!