http://mybatis.github.io/mybatis-3/zh/configuration.html 文档
架包:
http://pan.baidu.com/s/1dDH5fbj
1、创建 mybatis-config.xml文件,在该文件中完成和数据库的连接设置操作
2、根据数据表创建相应的实体类
3、创建 mapper文件完成对实体类的映射,该文件同样是 xml的文件
4、创建SqlSession,并且通过SqlSession完成对数据库的操作
5.最佳实践
1.创建相应的MyBatisUtil完成对Session的获取
2.不使用annotation完成sql的映射
1. 与model关联
TestMyBatis.java
package test;
import java.util.List;
import model.User;
import org.apache.ibatis.session.SqlSession;
import org.junit.After;
import org.junit.Test;
import util.MyBatisUtil;
public class TestMyBatis {
private SqlSession session = MyBatisUtil.createSession();
@After
public void tearDown()
{
session.commit();
MyBatisUtil. closeSession(session);
}
/**
* 删除
*/
@Test
public void testDelete()
{
session.delete(User.class.getName()+".delete",2);
}
/**
* 添加
*/
@Test
public void testAdd()
{
User user = new User();
user.setUsername("123" );
user.setPassword("123" );
user.setNickname("123" );
user.setType("123" );
session.insert(User.class.getName()+".add", user);
}
/**
* 修改
*/
@Test
public void testUpdate()
{
User user = new User();
user.setId(3);
user.setUsername("333" );
user.setPassword("333" );
user.setNickname("333" );
user.setType("123" );
session.update(User.class.getName()+".update", user);
}
/**
* 获取一个集合
*/
@Test
public void testLoad()
{
List<User> userList = session.selectList(User.class.getName()+".load");
for(User u : userList)
{
System. out.println(u );
}
}
}
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="model.User" >
<insert id="add" parameterType="model.User" >
insert into t_user (username,password,nickname,type)
value (#{username},#{password},#{nickname},#{type})
</insert >
<update id="update" parameterType="model.User" >
update t_user set password=#{password},nickname=#{nickname},type=#{type} where id=#{id}
</update >
<delete id="delete" parameterType="int" >
delete from t_user where id = #{id}
</delete >
<select id="load" parameterType="int" resultType="model.User" >
select * from t_user
</select >
</mapper>
2. 定义mapper接口
UserMapper.java
package mapper;
import java.util.List;
import model.User;
public interface UserMapper
{
public void add(User user );
public void update(User user );
public void delete(int id);
public List<User> load();
}
MapperTestMyBatis.java
package test;
import java.util.List;
import mapper.UserMapper;
import model.User;
import org.apache.ibatis.session.SqlSession;
import org.junit.After;
import org.junit.Test;
import util.MyBatisUtil;
public class MapperTestMyBatis {
private SqlSession session = MyBatisUtil.createSession();
@After
public void tearDown()
{
session.commit();
MyBatisUtil. closeSession(session);
}
/**
* 删除
*/
@Test
public void testDelete()
{
session.getMapper(UserMapper.class).delete(3);
}
/**
* 添加
*/
@Test
public void testAdd()
{
User user = new User();
user.setUsername("123" );
user.setPassword("123" );
user.setNickname("123" );
user.setType("123" );
session.getMapper(UserMapper.class).add(user);
}
/**
* 修改
*/
@Test
public void testUpdate()
{
User user = new User();
user.setId(3);
user.setUsername("333" );
user.setPassword("333" );
user.setNickname("333" );
user.setType("123" );
session.getMapper(UserMapper.class).update(user);
}
/**
* 获取一个集合
*/
@Test
public void testLoad()
{
List<User> userList = session.getMapper(UserMapper.class).load();
for(User u : userList)
{
System. out.println(u );
}
}
}
MyBatisUtil.java
package util;
import java.io.IOException;
import java.io.InputStream;
import org.apache.ibatis.io.Resources;
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;
import org.apache.ibatis.session.SqlSessionFactoryBuilder;
public class MyBatisUtil
{
private static SqlSessionFactory factory;
static
{
try {
InputStream is = Resources.getResourceAsStream("mybatis-config.xml");
factory = new SqlSessionFactoryBuilder().build(is );
} catch (IOException e ) {
e.printStackTrace();
}
}
public static SqlSession createSession()
{
return factory .openSession();
}
public static void closeSession(SqlSession session)
{
if(session != null)
session.close();
}
}
User
package model;
public class User
{
private int id ;
private String username;
private String password;
private String nickname;
private String type;
public int getId() {
return id ;
}
public void setId(int id) {
this.id = id;
}
public String getUsername() {
return username ;
}
public void setUsername(String username ) {
this.username = username;
}
public String getPassword() {
return password ;
}
public void setPassword(String password ) {
this.password = password;
}
public String getNickname() {
return nickname ;
}
public void setNickname(String nickname ) {
this.nickname = nickname;
}
public String getType() {
return type ;
}
public void setType(String type ) {
this.type = type;
}
@Override
public String toString() {
return "User [id=" + id + ", username=" + username + ", password="
+ password + ", nickname=" + nickname + ", type=" + type + "]";
}
}
mybatis-config.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 namespace -->
<!-- <typeAliases>
<typeAlias type="model.User" alias="user"/>
</typeAliases> -->
<environments default="development">
<environment id= "development">
<transactionManager type="JDBC" ></transactionManager>
<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 >
<!-- 将mapper文件加入配置文件中 -->
<mappers >
<mapper resource="model/UserMapper.xml" />
</mappers >
</configuration>
jdbc.properties
url=jdbc:mysql://localhost:3306/db_shop?useUnicode=true&characterEncoding= utf-8
driver=com.mysql.jdbc.Driver
username=root
password=root
源码:http://pan.baidu.com/s/1dD2CaLb