码迷,mamicode.com
首页 > 其他好文 > 详细

Mybatis搭建

时间:2020-02-10 11:41:15      阅读:54      评论:0      收藏:0      [点我收藏+]

标签:try   nic   nsa   list   odi   数据源   mys   oct   ssi   

java环境准备:

开发环境搭建

Mybatis搭建步骤:

1、导入jar包

2、创建mybatis-config.xml配置数据源

3、创建工具类,将mybatis-config.xml配置文件写入SqlSessionFactory,打开数据连接,生成SqlSession

4、创建pojo类,创建dao接口,创建mapping文件

5、编写测试类实现

 

1、创建pojo类User

public class User {
    private int id;  //id
    private String name;   //姓名
    private String pwd;   //密码

//构造,有参,无参
    //set/get
    //toString()
}

2、创建UserDao接口

public interface UserDao {
    List<User> selectUser();
}

3、创建mapping.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.alan.dao.UserDao">
    <select id="selectUser" resultType="com.alan.pojo.User">
        select * from user
    </select>
</mapper>

4、创建mybatis-config.xml配置文件,配置数据源,注册mapping

<?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/mybatis?useSSL=true&amp;useUnicode=true&amp;characterEncoding=utf8"/>
                <property name="username" value="root"/>
                <property name="password" value="1qazxsw2"/>
            </dataSource>
        </environment>
    </environments>
    <mappers>
        <mapper resource="com/alan/dao/mapping.xml"/>
    </mappers>
</configuration>

5、创建工具类MybatisUtils

public class MybatisUtils {

    private static SqlSessionFactory sqlSessionFactory;

    static {
        try {
            String resource = "mybatis-config.xml";
            InputStream inputStream = Resources.getResourceAsStream(resource);
            sqlSessionFactory = new SqlSessionFactoryBuilder().build(inputStream);
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    //获取SqlSession连接
    public static SqlSession getSession(){
        return sqlSessionFactory.openSession();
    }

}

6、创建测试类

public class MyTest {
    @Test
    public void selectUser() {
        SqlSession session = MybatisUtils.getSession();
        //方法一:
        //List<User> users = session.selectList("com.alan.mapper.UserDao.selectUser");
        //方法二:
        UserDao mapper = session.getMapper(UserDao.class);
        List<User> users = mapper.selectUser();

        for (User user: users){
            System.out.println(user);
        }
        session.close();
    }
}

 

Mybatis搭建

标签:try   nic   nsa   list   odi   数据源   mys   oct   ssi   

原文地址:https://www.cnblogs.com/alanchenjh/p/12290037.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!