第一个Mybatis项目(查询表User中所有数据)

1、在pom.xml中配置:

//依赖配置:
<dependencies>
    <dependency>
        <groupId>org.mybatis</groupId>
        <artifactId>mybatis</artifactId>
        <version>3.4.4</version>
    </dependency>
    <dependency>
        <groupId>mysql</groupId>
        <artifactId>mysql-connector-java</artifactId>
        <version>5.1.6</version>
    </dependency>
    <dependency>
        <groupId>junit</groupId>
        <artifactId>junit</artifactId>
        <version>4.12</version>
    </dependency>
</dependencies>

//防止资源文件导出失败:
<build>
    <resources>
        <resource>
            <directory>src/main/resources</directory>
            <includes>
                <include>**/*.properties</include>
                <include>**/*.xml</include>
            </includes>
            <filtering>true</filtering>
        </resource>
        <resource>
            <directory>src/main/java</directory>
            <includes>
                <include>**/*.properties</include>
                <include>**/*.xml</include>
            </includes>
            <filtering>true</filtering>
        </resource>
    </resources>
</build>

2、创建mybatis-config.xml文件,一般放入maven项目中的resource目录中:
<?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?useSSL=true&amp;useUnicode=true&amp;characterEncoding=UTF-8&amp;serverTimezone=GMT%2B8"/>
                <property name="username" value="root"/>
                <property name="password" value="root"/>
            </dataSource>
        </environment>
    </environments>
</configuration>

其中driver为jdbc驱动:com.mysql.jdbc.Driver,url为数据库地址,username为用户名,password为密码。

2、编写mybatis工具类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();
        }
    }
    public static SqlSession getSqlSession(){
        return sqlSessionFactory.openSession();
    }
}
//以上内容基本上是写死了的

3、创建一个javabean,字段与表中的字段一致。
public class User {
    private String username;
    private String pswd;
    private String sex;
    public User(){}

    public User(String username, String pswd, String sex) {
        this.username = username;
        this.pswd = pswd;
        this.sex = sex;
    }

    public String getUsername() {
        return username;
    }

    public void setUsername(String username) {
        this.username = username;
    }

    public String getPswd() {
        return pswd;
    }

    public void setPswd(String pswd) {
        this.pswd = pswd;
    }

    public String getSex() {
        return sex;
    }

    public void setSex(String sex) {
        this.sex = sex;
    }

    @Override
    public String toString() {
        return "User{" +
                "username='" + username + '\'' +
                ", pswd='" + pswd + '\'' +
                ", sex='" + sex + '\'' +
                '}';
    }
}
4、编写UserMapper接口:
public interface UserMapper {
    public List getUserList();
}

5、编写UserMapper接口对应的xml:UserMapper.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="dao.UserMapper">
    <select id="getUserList" resultType="pojo.User">
        select * from mybatis.user
    </select>
</mapper>

注意:namespace绑定UserMapper接口,id与接口中的getUserList()方法关联,resultType绑定刚才编写的javabean:User
select标签内容为sql查询语句:select * from mybatis.user
6、去mybatis-config.xml中把刚才写的UserMapper.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?useSSL=true&amp;useUnicode=true&amp;characterEncoding=UTF-8&amp;serverTimezone=GMT%2B8"/>
                <property name="username" value="root"/>
                <property name="password" value="root"/>
            </dataSource>
        </environment>
    </environments>
    <mappers>
        <mapper resource="./dao/UserMapper.xml" />
    </mappers>
</configuration>

可以发现就是使用了mappers,mapper标签:
<mappers>
    <mapper resource="./dao/UserMapper.xml" />
</mappers>

7、编写测试类进行验证:

我这里使用junit:
@Test
public void test(){
    SqlSession sqlSession = new MybatisUtils().getSqlSession();
    UserMapper userMapper = sqlSession.getMapper(UserMapper.class);
    List<User> userList = userMapper.getUserList();
    for(User user:userList){
        System.out.println(user);
    }
    sqlSession.close();
}

结果:

8、最后附上一张项目结构图以作参考:



 
posted @ 2020-08-29 09:48  曾经沧海难为水。  阅读(2473)  评论(0编辑  收藏  举报