现在的位置: 首页 > 综合 > 正文

mybatis 之 环境搭建

2013年05月05日 ⁄ 综合 ⁄ 共 2748字 ⁄ 字号 评论关闭

配置文件 mybatis-cofig.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"/>
	<typeAliases>
		<package name="itat.zttc.shop.model"/><!-- 设置了package后下面的别名就不用单个设置了 -->
		<!-- <typeAlias type="itat.zttc.shop.model.User" alias="User"/> --> <!-- 设置别名 可以直接在映射文件中的参数类型中使用别名-->
	</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="itat/zttc/shop/model/User.xml" />
	</mappers>
</configuration>

映射文件User.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="itat.zttc.shop.model.User">  <!-- namespace 是自定义的  --> 
	<insert id="add" parameterType="User">  <!-- 使用了 itat.zttc.shop.model.User的别名-->
		insert into t_user (username,password,nickname,type) value 
			(#{username},#{password},#{nickname},#{type})
	</insert>
	
	<update id="update" parameterType="User">
		update t_user set nickname=#{nickname},password=#{password},type=#{type} where id=#{id}
	</update>
	
	<select id="load" parameterType="int" resultType="User"> <!-- parameterType:参数类型,resultTyp:返回值类型 -->
		select * from t_user where id=#{id};
	</select>
	
	<select id="list" resultType="User">
		select * from t_user
	</select>
</mapper>

操作

public class TestSql {
	private SqlSession session=null;
	@Before
	public void init(){
		try {
			//读取配置文件
			String resource = "mybatis-config.xml";
			InputStream inputStream = Resources.getResourceAsStream(resource);
			//建立session
			SqlSessionFactory factory= new SqlSessionFactoryBuilder().build(inputStream);
			session=factory.openSession();
		} catch (IOException e) {
			e.printStackTrace();
		}
	}
	
	@Test
	public void add(){
		User u=new User();
		u.setNickname("校友");
		u.setUsername("ljf");
		u.setPassword("123");
		
		session.insert(User.class.getName()+".add", u);
		session.commit();
		session.close();
	}
	
	@Test
	public void load(){
		User u=session.selectOne(User.class.getName()+".load", 3);
		System.out.println(u.getNickname());
	}
	
	@Test
	public void list(){
		List<User> list=session.selectList(User.class.getName()+".list");
		System.out.println(list.size());
	}
	@Test
	public void test(){
		System.out.println(User.class.getName());
	}
}

在mybatis中还有一种常用Mapper操作的方式,略。

操作数据时,可以使用注解方式,不使用xml再写映射文件了,虽然注解在其它框架很常用(hibernate,spring),但mybatis功能还是用xml方式更强大

分页注意的事项:

<!-- 下面的注意,#{param}使用?進行替代加 '',${param}完整將字符串完成替代 不加''-->
	<select id="find" resultType="User" parameterType="map">
		select * from t_user where (username like #{name} or nickname like #{name})
		order by ${sort} ${order}
		limit #{pageOffset},#{pageSize}
	</select>

抱歉!评论已关闭.