日期:2014-05-16  浏览次数:20599 次

Mybatis3快速上手应用示例

基本步骤

?

  1. 加入jar包
  2. 定义dao接口
  3. 定义配置文件
  4. 实现dao接口

?


快速示例

?

环境

mysql数据库,已经表user,内有字段id,name,mark,其中id为主键,并且是自动递增类型

?

CREATE TABLE `user` (
  `id` int(11) NOT NULL AUTO_INCREMENT,
  `name` varchar(20) DEFAULT NULL,
  `mark` varchar(100) DEFAULT NULL,
  PRIMARY KEY (`id`)
) ENGINE=InnoDB AUTO_INCREMENT=5 DEFAULT CHARSET=utf8;
?

?

?

1.所需JAR包

mybatis-3.0.5.jar

?

2.对象实体类?User.java 包路径testMybatis.user

?

package testMybatis.user;

public class User {
    Long id;
    String name;
    String mark;

    public Long getId() {
        return id;
    }

    public void setId(Long id) {
        this.id = id;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public String getMark() {
        return mark;
    }

    public void setMark(String mark) {
        this.mark = mark;
    }

    @Override
    public String toString() {
        return "User [id=" + id + ", name=" + name + ", mark=" + mark + "]";
    }

}
?

?

?

3.DAO层接口类?TestDao.java?包路径testMybatis.user

?

package testMybatis.user;

public interface TestDao {
    public int insert(User user);
    public User select(long id);
    public List<User> selectAll();
    public int delete(long id);
}
?

4.定义配置文件

(注意:文件位置与接口在同一位置,文件名与接口同名,后缀以.xml结尾)

示例文件TestDao.xml 位置 testMybatis.user;

?

<?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="testMybatis.user.TestDao">
	<insert id="insert" parameterType="testMybatis.user.User" useGeneratedKeys="true" keyProperty="id">
		insert into user   (name,mark)
					values (#{name},#{mark})
	</insert>
	
	<select id="select" parameterType="long" resultType="testMybatis.user.User">
		select * from user
			   where name.id=#{id}
	</select>
	
	<select id="selectAll"  resultType="testMybatis.user.User" >
		select * from user
	</select>
	
	<delete id="delete" parameterType="long">
		delete from user
		where id = #{id}
	</delete>
</mapper>
?

5.实现类TestDaoImp.java

(注意:本示例为快速测试使用的是编码配置数据源工厂,一般使用文件配置)

package testMybatis.user;

import java.util.List;

/**
 * @author DingFengHua
 * @since Apr 16, 2012
 */
public class TestDaoImp implements TestDao {
//当前用于快速测试,一般设计成单例使用工具类来调用
    static SqlSessionFactory sqlSessionFactory;

    static {
        DataSource dataSource = new PooledDataSource("com.mysql.jdbc.Driver",
                        "jdbc:mysql://localhost/mybatis?userUnicode=true&amp;characterEncoding=utf8", "admin", "123456");
        Environment environment = new Environment("test", new JdbcTransactionFactory(), dataSource);
        Configuration configuration = new Configuration(environment);
        configuration.addMapper(TestDao.class);
        sqlSessionFactory = new SqlSessionFactoryBuilder().build(configuration);

    }

    /*
     * (non-Javadoc)
     * @see testMybatis.user.testDao#insertName(testMybatis.user.User)
     */
    @Override
    public int insert(User user) {
        SqlSession session = sqlSessionFactory.openSession();
        int num = 0;
        try {
            TestDao dao = session.getMapper(TestDao.class);