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

mybatis 3.2.2_环境搭建

时间:2015-03-15 22:42:50      阅读:178      评论:0      收藏:0      [点我收藏+]

标签:

1.创建一个工程 utf-8

2、导入jar

mybatis-3.2.2.jar 核心包

依赖包:

asm-3.3.1.jar

cglib-2.2.2.jar

commons-logging-1.1.1.jar

javassist-3.17.1-GA.jar

log4j-1.2.17.jar

slf4j-api-1.7.5.jar

slf4j-log4j12-1.7.5.jar

3、配置核心文件 sqlMapConfig.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>
	<environments default="development">
		<!-- 连接数据库的开发环境 -->
		<environment id="development">
			<transactionManager type="JDBC"/>
			<dataSource type="POOLED">
				<property name="driver" value="com.mysql.jdbc.Driver"/>
				<property name="url" value="jdbc:mysql://localhost:3306/mybatisdb?characterEncoding=utf-8"/>
				<property name="username" value="root"/>
				<property name="password" value="root"/>
			</dataSource>
		</environment>
		<!-- 连接数据库的部署环境 -->
		<environment id="product">
			<transactionManager type="JDBC"/>
			<dataSource type="POOLED">
				<property name="driver" value="com.mysql.jdbc.Driver"/>
				<property name="url" value="jdbc:mysql://localhost:3306/mybatisdbproduct?characterEncoding=utf-8"/>
				<property name="username" value="root"/>
				<property name="password" value="root"/>
			</dataSource>		
		</environment>
	</environments>
	
	<mappers>
		<mapper resource="com/mapping/UserMapper.xml"/>
	</mappers>
</configuration>

 

4、创建数据库,创建表USER

create table USER_C
(
   ID                   varchar(40) not null,
   NAME            varchar(30),
   AGE                int,
   ADDRESS       varchar(200),
   primary key (ID)
);

--加入测试数据

insert  into `user_c`(`id`,`name`,`age`,`address`) 
values (‘1‘,‘夏言‘,73,‘桂州村‘),
(‘2‘,‘严嵩‘,87,‘分宜县城介桥村‘),
(‘3‘,‘徐阶‘,80,‘明松江府华亭县‘),
(‘4‘,‘高拱‘,66,‘河南省新郑市高老庄村‘),
(‘5‘,‘张居正‘,58,‘江陵‘);

 
5、映射文件 UserMapper.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="com.domain.User">
	<sql id="createcols">
		#{id},#{name},#{age},#{address}
	</sql>
	<!-- 查询所有记录 -->
	<select id="listAll" resultType="com.domain.User">
		select * from user_c
	</select>
	<!-- 查询一条 -->
	<select id="get" parameterType="string" resultType="com.domain.User">
		SELECT id,name,age,address FROM user_c WHERE id=#{id}
	</select>
	<!-- 新增记录 -->
	<insert id="create" parameterType="com.domain.User">
		insert into user_c 
		 (id,name,age,address) 
		 values( <include refid="createcols"/> )
	</insert>
	
	<!-- 删除记录 -->
	<delete id="delete" parameterType="string">
		delete from user_c where id=#{id}
	</delete>
</mapper>

 6.PO对象

package com.domain;

public class User {
	private String id;
	private String name;
	private Integer age;
	private String address;
	public String getId() {
		return id;
	}
	public void setId(String id) {
		this.id = id;
	}
	public String getName() {
		return name;
	}
	public void setName(String name) {
		this.name = name;
	}
	public Integer getAge() {
		return age;
	}
	public void setAge(Integer age) {
		this.age = age;
	}
	public String getAddress() {
		return address;
	}
	public void setAddress(String address) {
		this.address = address;
	}
	public String toString(){
		return "id =" + id + 
				";name" + name + 
				";address=" + address + 
				";age=" + age;
	}
	
	
}

 7.写测试类

package com.test;

import java.io.IOException;
import java.io.InputStream;
import java.util.List;
import java.util.UUID;

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.Before;
import org.junit.Test;

import com.domain.User;

public class TestUser {
	SqlSessionFactory sqlSessionFactory;
	
	@Before
	public void initFactory() throws IOException{
		String configPath = "sqlMapConfig.xml";
		InputStream in = Resources.getResourceAsStream(configPath);
		sqlSessionFactory = new SqlSessionFactoryBuilder().build(in);
	}
	@Test
	public void testListAll(){
		SqlSession sqlSession = sqlSessionFactory.openSession();
		List<User> list = sqlSession.selectList("com.domain.User.listAll");
		System.out.println(list.size());
	}
	@Test
	public void testGetUser(){
		SqlSession sqlSession = sqlSessionFactory.openSession();
		User user = sqlSession.selectOne("com.domain.User.get","3");
		System.out.println(user.toString());
	}
	
	@Test //新增记录
	public void testCreate(){
		SqlSession session = sqlSessionFactory.openSession();		//获得SqlSession
		User u  = new User();
		u.setId(UUID.randomUUID().toString());		//产生UUID主键
		u.setName("jack");
		u.setAge(22);
		u.setAddress("beijing");
		
		int i = session.insert("com.domain.User.create", u);
		session.commit();
		System.out.println("影响条数:"+i);
		
	}
	@Test //新增记录
	public void testDelete(){
		SqlSession session = sqlSessionFactory.openSession();		//获得SqlSession
		int i = session.delete("com.domain.User.delete", "");
		session.commit();
		System.out.println("影响条数:"+i);
		
	}

}

 

mybatis 3.2.2_环境搭建

标签:

原文地址:http://www.cnblogs.com/zhangshiwen/p/4340420.html

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