Mybatis入门配置及第一个Mybatis程序

目的:使用mybatis来进行对数据库表的操作

第一步:引入jar包

我这里是创建的maven工程

第二步:创建数据表user

第三步:创建实体类

 实体类放在包 com.xxx.pojo 下,包名可自行修改。实体类中属性名可以和数据库中字段名一致,也可以不一致,推荐一致

public class User {

    private int id;
    private String user_name;
    private String user_sex;
    private int user_age;

    public int getId() {
        return id;
    }

    public void setId(int id) {
        this.id = id;
    }

    public String getUser_name() {
        return user_name;
    }

    public void setUser_name(String user_name) {
        this.user_name = user_name;
    }

    public String getUser_sex() {
        return user_sex;
    }

    public void setUser_sex(String user_sex) {
        this.user_sex = user_sex;
    }

    public int getUser_age() {
        return user_age;
    }
    public void setUser_age(int user_age) {
        this.user_age = user_age;
    }
    @Override
    public String toString() {
        return "User [id=" + id + ", user_name=" + user_name + ", user_sex=" + user_sex + ", user_age=" + user_age
                + "]";
    }
    public User(String user_name, String user_sex, int user_age) {
        super();
        this.user_name = user_name;
        this.user_sex = user_sex;
        this.user_age = user_age;
    }
    public User() {
        super();
    }
}

 

 第四步:配置Mybatis核心配置文件

Mybatis核心配置文件可以任意取名,也可以放在任意路径下。在这里,我的配置文件名称是:mybatis-config.xml,而且是放在了src下

<?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>
    <!-- 配置数据库环境  default:指定默认的数据库-->
    <environments default="mysql">
        <!-- id:数据库的名称,唯一的 -->
        <environment id="mysql">
            <!-- 事务管理 交给jdbc管理-->
            <transactionManager type="jdbc"></transactionManager>
            <dataSource type="POOLED">
                <property name="driver" value="com.mysql.jdbc.Driver"/>
                <property name="url" value="jdbc:mysql://localhost:3306/test"/>
                <property name="username" value="root"/>
                <property name="password" value="123456"/>
            </dataSource>
        </environment>
        
        <environment id="oracle">
            <transactionManager type="jdbc"></transactionManager>
            <dataSource type="POOLED">
                <property name="driverClassName" value="oracle.jdbc.driver.OracleDriver"/>
                <property name="url" value="jdbc:oracle:thin:@//localhost:1521/数据库名"/>
                <property name="username" value="用户名"/>
                <property name="password" value="密码"/>
            </dataSource>
        </environment>
    </environments>
    
    <!-- 映射文件 -->
    <mappers>
        <mapper resource="com/xxx/pojo/UserMapper.xml"/>
    </mappers>
    
</configuration>

 在编写核心配置文件的时候,你可能会发现 alt+/ 没有给出提示,这是因为你没有将mybatis的核心配置文件的约束引入eclipse,引入方法:

在浏览器上地址栏输入:http://mybatis.org/dtd/mybatis-3-config.dtd,会进入 dtd 约束文件下载,接着点击window--->preferences-->搜索xml--->选择xml Catelog

点击 add

Location:你下载的 dtd 文件的的路径

Key type:URI

Key:http://mybatis.org/dtd/mybatis-3-config.dtd

第五步:编写实体类映射文件 

<?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.xxx.pojo.User">
    <!-- 对返回值数据的映射规则  -->
    <resultMap type="com.xxx.pojo.User" id="UserMap">
        <id property="id" column="id"/>
        <result property="user_name" column="user_name"/>
        <result property="user_sex" column="user_sex"/>
        <result property="user_age" column="user_age"/>
    </resultMap>
    
    
    <!-- 编写插入语句 parameterType:传入的参数类型-->
    <insert id="insert1" parameterType="com.xxx.pojo.User">
        insert into user(user_name,user_sex,user_age)values(#{user_name},#{user_sex},#{user_age})
    </insert>
    
    <update id="update1" parameterType="com.xxx.pojo.User">
        update user set user_name=#{user_name},user_sex=#{user_sex},user_age=#{user_age} where id=#{id}
    </update>
    
    <delete id="delete1" parameterType="int">
        delete from user where id=#{id}
    </delete>
    
    <select id="select1" resultMap="UserMap">
        select *from user
        
    </select>
    
</mapper>

 

第六步:测试

编写测试类,进行测试框架

package com.xxx.test;

import java.io.IOException;
import java.io.InputStream;
import java.util.ArrayList;
import java.util.List;

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 com.xxx.pojo.User;

public class TestCase {
    
    private static SqlSession session;
    
    public static SqlSession getSqlSession(){
        try {
       //读取mybatis核心配置文件 InputStream inputStream
= Resources.getResourceAsStream("mybatis-config.xml");
       //获取SqlSessionFactory工厂对象 SqlSessionFactory sqlSessionFactory
= new SqlSessionFactoryBuilder().build(inputStream);
       //获取SqlSession对象 session
= sqlSessionFactory.openSession(); } catch (IOException e) { e.printStackTrace(); } return session; } public static void close(){ if(session!=null){ session.close(); } } /* * 测试插入 */ @Test public void test1(){ SqlSession sqlSession = getSqlSession(); User user = new User("张三", "男", 20); sqlSession.insert(User.class.getName()+".insert1", user);
     //每次对数据库发生改变之后,不要忘记提交 sqlSession.commit(); close(); }
/* * 测试更新数据 */ @Test public void test2(){ SqlSession sqlSession = getSqlSession(); User user = new User("张三", "男", 20); user.setUser_age(30); user.setId(4); sqlSession.update("com.wangtong.pojo.User.update1", user); sqlSession.commit(); close(); } /* * 测试删除 */ @Test public void test3(){ SqlSession sqlSession = getSqlSession(); sqlSession.delete(User.class.getName()+".delete1", 5); sqlSession.commit(); close(); } /* * 测试查询 */ @Test public void test4(){ SqlSession sqlSession = getSqlSession(); List<User> list = sqlSession.selectList(User.class.getName()+".select1"); System.out.println(list); sqlSession.commit(); close(); } }

 

posted @ 2019-08-20 15:54  BeenTogether  阅读(193)  评论(0编辑  收藏  举报