pinked

导航

第一个mybatis程序

第一个mybatis程序

搭建环境->导入mybatis->代码->测试

1.搭建环境

搭建数据库

CREATE DATABASE `mybatis`;
USE `mybatis`;

CREATE TABLE `user`(
  `id` INT(20) NOT NULL PRIMARY KEY,
  `name` VARCHAR(30) DEFAULT NULL,
  `pwd` VARCHAR(30) DEFAULT NULL
  )ENGINE=INNODB DEFAULT CHARSET=utf8;
  
  
INSERT INTO `user`(`id`,`name`,`pwd`) VALUES
(1,'wo','123456'),
(2,'ta','123456'),
(3,'zhangsan','123456')

新建项目

​ 1.新建一个普通的maven项目

​ 2.删除src目录

​ 3.导入依赖

<dependencies>
        <dependency>
            <groupId>mysql</groupId>
            <artifactId>mysql-connector-java</artifactId>
            <version>8.0.18</version>
        </dependency>
        <!-- https://mvnrepository.com/artifact/org.mybatis/mybatis -->
        <dependency>
            <groupId>org.mybatis</groupId>
            <artifactId>mybatis</artifactId>
            <version>3.5.3</version>
        </dependency>        <dependency>
            <groupId>junit</groupId>
            <artifactId>junit</artifactId>
        <version>4.12</version>
    </dependency>
    </dependencies>

2.创建一个新的模块

  • 编写mybatis的核心配置文件

    <?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.cj.jdbc.Driver"/>
                    <property name="url" value="jdbc:mysql://localhost:3306/mybatis?useUnicode=ture&amp;characterEncoding=UTF-8"/>
                    <property name="username" value="root"/>
                    <property name="password" value="123456"/>
                </dataSource>
            </environment>
        </environments>
    </configuration>				
    
  • 编写mybatis工具类

    public class MybatisUtils {
        private static SqlSessionFactory sqlSessionFactory;
        static {
            try {
    //            使用mybatis第一步:获取sqlSessionFactory对象
                String resource = "mybatis-config.xml";
                InputStream inputStream = Resources.getResourceAsStream(resource);
                sqlSessionFactory = new SqlSessionFactoryBuilder().build(inputStream);
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    //    sqlSession包含了面向数据库执行sql命令所需的所有方法
        public static SqlSession getSqlSession(){
            return sqlSessionFactory.openSession();
        }
    }
    

3.编写代码

  • 实体类

    public class User {
        private int id;
        private String name;
        private String pwd;
    
        public User() {
        }
    
        public User(int id, String name, String pwd) {
            this.id = id;
            this.name = name;
            this.pwd = pwd;
        }
    
        public int getId() {
            return id;
        }
    
        public void setId(int id) {
            this.id = id;
        }
    
        public String getName() {
            return name;
        }
    
        public void setName(String name) {
            this.name = name;
        }
    
        public String getPwd() {
            return pwd;
        }
    
        public void setPwd(String pwd) {
            this.pwd = pwd;
        }
    
        @Override
        public String toString() {
            return "User{" +
                    "id=" + id +
                    ", name='" + name + '\'' +
                    ", pwd='" + pwd + '\'' +
                    '}';
        }
    }
    
  • Dao接口

    public interface UserDao {
        List<User> getUserList();
    }
    
  • 接口实现类

    <?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">
    <!--namespace绑定对应的Dao/Mapper接口-->
    <mapper namespace="cn.pinked.dao.UserDao">
        <select id="getUserList" resultType="cn.pinked.pojo.User">
            select * from mybatis.user
        </select>
    </mapper>
    

4.测试

  • junit

    @Test
        public void Test(){
    //        获得SqlSession对象
            SqlSession sqlSession = MybatisUtils.getSqlSession();
    //        方式:getMapper
            UserDao userDao = sqlSession.getMapper(UserDao.class);
            List<User> userList = userDao.getUserList();
    
            for (User user : userList) {
                System.out.println(user);
            }
    
            sqlSession.close();//关闭SqlSession
        }
    

posted on 2020-01-08 12:37  pinked  阅读(135)  评论(0编辑  收藏  举报