MyBatis开发环境搭建

参考文档地址:http://www.mybatis.org/mybatis-3/zh/index.html

项目地址:https://github.com/mybatis/mybatis-3

什么是 MyBatis?

  MyBatis 是一款优秀的持久层框架,它支持定制化 SQL、存储过程以及高级映射。MyBatis 避免了几乎所有的 JDBC 代码和手动设置参数以及获取结果集。MyBatis 可以使用简单的 XML 或注解来配置和映射原生类型、接口和 Java 的 POJO(Plain Old Java Objects,普通老式 Java 对象)为数据库中的记录。

1. 依赖包:

<dependency>
<groupId>org.mybatis</groupId>
<artifactId>mybatis</artifactId>
<version>3.5.1</version>
</dependency>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>5.1.47</version>
</dependency>

2. 创建MyBatis核心配置文件“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>
    <!-- 1.全局配置文件 -->
    <properties resource="config/database.properties"></properties>
    <!-- 2.批量别名 -->
    <typeAliases>
        <package name="cn.ll.mybatis.vo"/>
    </typeAliases>
    <environments default="development">
        <environment id="development">
            <transactionManager type="JDBC"/>
            <dataSource type="POOLED">
                <property name="driver" value="${datasource.driver}"/>
                <property name="url" value="${datasource.url}"/>
                <property name="username" value="${datasource.username}"/>
                <property name="password" value="${datasource.password}"/>
            </dataSource>
        </environment>
    </environments>
    <!-- 3.基于包扫描,批量注册映射文件 -->
    <mappers>
        <!--<mapper resource="EmployeeMapper.xml" />-->
        <!--<mapper class="cn.ll.mybatis.dao.EmployeeMapper" />-->
        <package name="cn.ll.mybatis.dao"/>
    </mappers>
</configuration>

3. 创建映射文件:

<?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="cn.ll.mybatis.dao.EmployeeMapper"> <select id="getEmpById" resultType="Employee"> select * from employee where eid = #{id} </select> <insert id="add" useGeneratedKeys="true" keyProperty="eid" > INSERT INTO employee(name,gender,email) VALUES(#{name},#{gender},#{email}) </insert> <update id="edit" > UPDATE employee SET name=#{name},gender=#{gender},email=#{email} WHERE eid=#{eid} </update> <delete id="remove" > DELETE FROM employee WHERE eid=#{eid} </delete> </mapper>

4. 加载配置文件,进行测试:

@Test
public void test() throws Exception {
    String resource = "mybatis/mybatis-config.xml";
    InputStream inputStream = Resources.getResourceAsStream(resource);
    //1、根据xml配置文件(全局配置文件)创建一个SqlSessionFactory对象
    SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(inputStream);
    //2、获取SqlSession实例,能直接执行已经映射的sql语句,包括:sql的唯一标识,执行sql要用的参数
    SqlSession session = sqlSessionFactory.openSession();
    try {
        Employee employee = session.selectOne("cn.ll.mybatis.dao.EmployeeMapper.getEmpById",4);
        System.out.println(employee);
    } finally {
        session.close();
    }
}

 

posted @ 2019-06-25 01:34  SweetBaby。  阅读(308)  评论(0编辑  收藏  举报