mybatis mysql db 访问 demo

2017-06-25

114

0

技术:mybatis.3.2.3 + windows mysql.5.6.24 

运行环境:IDEA 15.2 + jdk8 + windows 10

demo功能:提供一个mybatis访问mysql的基本代码demo

新建实体类和数据库访问dao接口

//字段和数据库表字段保持一致, 不然就得使用表字段和类字段映射, 个人强烈不推荐
public class User {
    private String id;
    private String name;
    private String sex;
    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 String getSex() {
        return sex;
    }
    public void setSex(String sex) {
        this.sex = sex;
    }
}
//注意这里只是申请interface
public interface IUserDao {
    User getById(String id);
    void add(User user);
    void updateName(User user);注意参数传递
}

添加db配置文件和sql配置文件.全部放在resource中或者任何classpath下都行

mybatis-conf.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/test?useUnicode=true&amp;characterEncoding=utf-8"/>
                <property name="username" value="root"/>
                <property name="password" value="123456"/>
            </dataSource>
        </environment>
    </environments>
    <mappers>
        <mapper resource="mappers/user.maper.xml"/>
    </mappers>
</configuration>

mappers/user.maper.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.demoworld.dao.IUserDao">
    <resultMap id="list" type="com.demoworld.entity.User">
        <id column="id"></id>
    </resultMap>
    <select id="getById" parameterType="String" resultType="com.demoworld.entity.User">
        select id, name, sex from tb_user
        where id=#{id}
    </select>
    <insert id="add" keyColumn="id" parameterType="com.demoworld.entity.User" flushCache="true">
        insert into tb_user values(#{id}, #{name}, #{sex})
    </insert>
    <update id="updateName" parameterType="com.demoworld.entity.User" flushCache="true">
        update tb_user set name=#{name} where id=#{id}
    </update>
</mapper>

访问测试

@Test
public void add() throws IOException {
    //配置文件的名称
    String resource = "mybatis-conf.xml";
    //通过Mybatis包中的Resources对象很轻松的获取到配置文件
    Reader reader = Resources.getResourceAsReader(resource);//因为配置文件都在resource中或者classpath中,这里可以直接访问
    //通过SqlSessionFactoryBuilder创建
    SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(reader);
    //获得session实例
    SqlSession session = sqlSessionFactory.openSession();
    User user = new User();
    user.setId(UUID.randomUUID().toString());
    user.setName("francis");
    user.setSex("男");

    IUserDao mapper = session.getMapper(IUserDao.class);
    mapper.add(user);

    session.commit();
    session.close();
}

 

欢迎添加微信,互相学习↑↑↑ -_-

发表评论

全部评论:0条

白老虎

programming is not only to solve problems, ways to think