首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > 数据库 > 其他数据库 >

Mybatis3快速下手应用示例

2012-07-05 
Mybatis3快速上手应用示例基本步骤:?加入jar包定义dao接口定义配置文件实现dao接口?快速示例?环境mysql数

Mybatis3快速上手应用示例
基本步骤

?

    加入jar包定义dao接口定义配置文件实现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 userwhere 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);            num = dao.insert(user);            session.commit();        } finally {            session.close();        }        return num;    }    /*     * (non-Javadoc)     * @see testMybatis.user.testDao#select(long)     */    @Override    public User select(long id) {        SqlSession session = sqlSessionFactory.openSession();        try {            TestDao dao = session.getMapper(TestDao.class);            return dao.select(id);        } finally {            session.close();        }    }    /*     * (non-Javadoc)     * @see testMybatis.user.testDao#delete(long)     */    @Override    public int delete(long id) {        SqlSession session = sqlSessionFactory.openSession();        try {            TestDao dao = session.getMapper(TestDao.class);            int num = dao.delete(id);            session.commit();            return num;        } finally {            session.close();        }    }    /* (non-Javadoc)     * @see testMybatis.user.testDao#selectAll()     */    @Override    public List<User> selectAll() {        SqlSession session = sqlSessionFactory.openSession();        try {            TestDao dao = session.getMapper(TestDao.class);            return dao.selectAll();        } finally {            session.close();        }    }}
?

6.测试

public class Main {        public static void main(String... a) {        TestDao main = new TestDaoImp();        User user = new User();        user.setName("name1");        user.setMark("no mark");                System.out.println(main.insert(user));        //System.out.println(main.delete(1l));        for (User u : main.selectAll()) {            System.out.println(u);        }    }}
?

完成

热点排行