Mybatis_环境搭建

Posted aikang525

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Mybatis_环境搭建相关的知识,希望对你有一定的参考价值。

1.配置pom文件

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>

    <groupId>cn.mepu</groupId>
    <artifactId>mybatis_build</artifactId>
    <version>1.0-SNAPSHOT</version>
<!--指定打包方式-->
    <packaging>jar</packaging>
<!--    导入mybatis依赖-->
    <dependencies>
        <dependency>
            <groupId>org.mybatis</groupId>
            <artifactId>mybatis</artifactId>
            <version>3.5.3</version>
        </dependency>
<!--        导入mysql依赖-->
        <dependency>
            <groupId>mysql</groupId>
            <artifactId>mysql-connector-java</artifactId>
            <version>5.1.6</version>
        </dependency>
<!--        导入log4j-->
        <dependency>
            <groupId>log4j</groupId>
            <artifactId>log4j</artifactId>
            <version>1.2.16</version>
        </dependency>
<!--        导入junit-->
        <dependency>
            <groupId>junit</groupId>
            <artifactId>junit</artifactId>
            <version>4.10</version>
            <scope>test</scope>
        </dependency>
    </dependencies>

</project>

2.,创建数据库对应实体类实现Serializable

package cn.mepu.domain;

import java.io.Serializable;

/**
* @author shkstart
* @create 2019-11-04 9:45
*/
public class User implements Serializable {
private int id;
private String name;
private int age;

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 int getAge() {
return age;
}

public void setAge(int age) {
this.age = age;
}

@Override
public String toString() {
return "User{" +
"id=" + id +
", name=‘" + name + ‘ +
", age=" + age +
‘}‘;
}
}

3.创建Dao接口

package cn.mepu.Dao;

import cn.mepu.domain.User;

import java.util.List;

/**
 * @author shkstart
 * @create 2019-11-04 9:50
 */
public interface UserDao {
    /**
     * 查询所有用户
     * @return
     */
    List<User> findAll();
}

4.添加主配置文件SqlMapConifg.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="mysql">
        <environment id="mysql">
            <transactionManager type="JDBC"></transactionManager>
            <dataSource type="POOLED">
                <property name="driver" value="com.mysql.jdbc.Driver"/>
                <property name="url" value="jdbc:mysql://localhost:3306/javaee"/>
                <property name="username" value="root"/>
                <property name="password" value="root"/>
            </dataSource>
        </environment>
    </environments>
    <mappers>
<!--        定义配置映射-->
        <mapper resource="cn/mepu/Dao/UserDao.xml"></mapper>
    </mappers>
</configuration>

5.配置映射文件

 1 <?xml version="1.0" encoding="UTF-8" ?>
 2 <!DOCTYPE mapper
 3         PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
 4         "http://mybatis.org/dtd/mybatis-3-mapper.dtd">
 5 <!--定义映射接口-->
 6 <mapper namespace="cn.mepu.Dao.UserDao">
 7 <!-- id = dao接口方法名称 resultType=数据封装对象   -->
 8     <select id="findAll" resultType="cn.mepu.domain.User">
 9         select * from user 
10     </select>
11 </mapper>

 

6.测试类

 1 package cn.mepu;
 2 
 3 
 4 import cn.mepu.Dao.UserDao;
 5 import cn.mepu.domain.User;
 6 import org.apache.ibatis.io.Resources;
 7 import org.apache.ibatis.session.SqlSession;
 8 import org.apache.ibatis.session.SqlSessionFactory;
 9 import org.apache.ibatis.session.SqlSessionFactoryBuilder;
10 
11 import java.io.IOException;
12 import java.io.InputStream;
13 import java.util.List;
14 
15 /**
16  * @author shkstart
17  * @create 2019-11-04 10:02
18  */
19 public class mybatisTest {
20     public static void main(String[] args) throws IOException {
21         //1.读取配置文件
22         InputStream in = Resources.getResourceAsStream("SqlMapConifg.xml");
23         //2.创建SqlSessionFactory工厂
24         SqlSessionFactory factory = new SqlSessionFactoryBuilder().build(in);
25         //3.使用工厂生产SqlSession对象
26         SqlSession sqlSession = factory.openSession();
27         //4.使用SqlSession创建Dao接口代理对象
28         UserDao userDao = sqlSession.getMapper(UserDao.class);
29         //5.使用代理对象执行方法
30         List<User> users = userDao.findAll();
31         for (User user : users) {
32             System.out.println(user);
33         }
34         //6.释放资源
35         sqlSession.close();
36         in.close();
37     }
38 }

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

以上是关于Mybatis_环境搭建的主要内容,如果未能解决你的问题,请参考以下文章

阶段3 1.Mybatis_01.Mybatis课程介绍及环境搭建_05.mybatis环境搭建-前期准备

阶段3 1.Mybatis_01.Mybatis课程介绍及环境搭建_04.mybatis概述

阶段3 1.Mybatis_12.Mybatis注解开发_1 mybatis注解开发的环境搭建

[刘阳Java]_快速搭建MyBatis环境_第2讲

Mybatis_环境搭建

阶段3 1.Mybatis_01.Mybatis课程介绍及环境搭建_02.三层架构和ssm框架的对应关系