mybatis-入门项目(maven)

Posted ccoonngg

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了mybatis-入门项目(maven)相关的知识,希望对你有一定的参考价值。

.1.创建maven项目,在pom.xml中添加依赖

<?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>com.cong</groupId>
    <artifactId>mybatics_firstDemo</artifactId>
    <version>1.0-SNAPSHOT</version>

    <packaging>jar</packaging>
    <dependencies>
        <!-- mybatis核心包-->
        <dependency>
            <groupId>org.mybatis</groupId>
            <artifactId>mybatis</artifactId>
            <version>3.4.5</version>
        </dependency>
        <!-- mysql驱动包 -->
        <dependency>
            <groupId>mysql</groupId>
            <artifactId>mysql-connector-java</artifactId>
            <version>5.1.29</version>
        </dependency>
        <!-- junit测试包 -->
        <dependency>
            <groupId>junit</groupId>
            <artifactId>junit</artifactId>
            <version>4.10</version>
        </dependency>
    </dependencies>
</project>

2.mysql数据库相关操作

 

3..在java目录下创建com.cong.pojo.Account类

package com.cong.pojo;

public class Account 
    private int id;
    private String name;
    private float money;

    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 float getMoney() 
        return money;
    

    public void setMoney(float money) 
        this.money = money;
    

    @Override
    public String toString() 
        return "Account" +
                "id=" + id +
                ", name=‘" + name + ‘\\‘‘ +
                ", money=" + money +
                ‘‘;
    

4.在resources目录下创建mybatis-context.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>
    <!-- 环境配置 -->
    <typeAliases>
        <!-- 命名空间,这样在AccountMapper中返回值就不用写com.cong.pojo.Account,只要写Account就可以了 -->
        <package name="com.cong.pojo"></package>
    </typeAliases>
    <environments default="development">
        <environment id="development">
            <transactionManager type="JDBC"/>
            <!-- 数据库连接相关配置 ,这里动态获取config.properties文件中的内容-->
            <dataSource type="POOLED">
                <property name="driver" value="com.mysql.jdbc.Driver" />
                <property name="url" value="jdbc:mysql://localhost:3306/cong" />
                <property name="username" value="root" />
                <property name="password" value="123456" />
            </dataSource>
        </environment>
    </environments>
    <!-- mapping文件路径配置 -->
    <mappers>
        <mapper resource="mapper/AccountMapper.xml"/>
    </mappers>
</configuration>

5.在resources目录下创建mapper目录,在mapper目录下创建accountMapper.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接口的完整类名
    mybatis会依据这个接口动态创建一个实现类去实现这个接口,
    而这个实现类是一个Mapper对象
    -->
<mapper namespace="com.cong.pojo.Account">
    <select id="findAll" resultType="Account">
        select * from account
    </select>
    <select id="findById" parameterType="int" resultType="Account">
        select * from account where id = #id
    </select>
</mapper>

6.在test,java目录下创建AccountTest测试类

import com.cong.pojo.Account;
import org.apache.ibatis.io.Resources;
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;
import org.apache.ibatis.session.SqlSessionFactoryBuilder;
import org.junit.Test;

import java.io.IOException;
import java.io.Reader;
import java.util.List;

public class AccountTest 

    @Test
    public void accountFindById()
        SqlSession session = getSqlSession();
        //传入参数查询,返回结果
        Account account=session.selectOne("findById",1);
        //输出结果
        System.out.println(account.toString());
        //关闭session
        session.close();
    
    @Test
    public void accountFindAll()
        SqlSession session = getSqlSession();
        List<Account> list = session.selectList("findAll");
        for (Account account : list) 
            System.out.println(account.toString());
        
        session.close();
    
    private SqlSession getSqlSession() 
        //定义读取文件名
        String resources = "mybatis-config.xml";
        //创建流
        Reader reader=null;
        try 
            //读取mybatis-config.xml文件到reader对象中
            reader= Resources.getResourceAsReader(resources);
         catch (IOException e) 
            e.printStackTrace();
        
        //初始化mybatis,创建SqlSessionFactory类的实例
        SqlSessionFactory sqlMapper=new SqlSessionFactoryBuilder().build(reader);
        //创建session实例
        return sqlMapper.openSession();
    

7.findAll单元测试结果

技术图片

8.完整目录结构

技术图片

 

以上是关于mybatis-入门项目(maven)的主要内容,如果未能解决你的问题,请参考以下文章

MyBatis 框架之快速入门程序

Mybatis入门

MyBatis 框架之快速入门程序

MyBatis 入门

1MyBatis 快速入门

MyBatis系列:入门