码迷,mamicode.com
首页 > 其他好文 > 详细

mybatis简单示例

时间:2019-03-30 01:12:50      阅读:113      评论:0      收藏:0      [点我收藏+]

标签:new   factory   turn   char   builder   .sql   out   epo   jdbc   

 

1、引入mybatis.jar mysql-connector-java-5.1.47.jar

<dependency>
    <groupId>org.mybatis</groupId>
    <artifactId>mybatis</artifactId>
    <version>3.4.6</version>
</dependency>
<!-- https://mvnrepository.com/artifact/mysql/mysql-connector-java -->
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>5.1.47</version>
</dependency>

2、新增数据库资源文件datasource.properties

jdbc.driver=com.mysql.jdbc.Driver
jdbc.url=jdbc:mysql://localhost:3306/test?useUnicode=true&characterEncoding=UTF-8
jdbc.username=root
jdbc.password=

3、新增mybatis.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>
    <properties resource="datasource.properties"/>
    <environments default="dev">
        <environment id="dev">
            <transactionManager type="JDBC"></transactionManager>
            <dataSource type="POOLED">
                <property name="driver" value="${jdbc.driver}" />
                <property name="url" value="${jdbc.url}" />
                <property name="username" value="${jdbc.username}" />
                <property name="password" value="${jdbc.password}" />
            </dataSource>
        </environment>
    </environments>
    <mappers>
        <mapper resource="mapper/UserMapper.xml"/>
    </mappers>
</configuration>

4、新增UserDomain.java

package cn.scp.mybatis.native_mybatis.domain;

import lombok.Data;

import java.util.Date;

@Data
public class UserDomain {
    private int id;
    private String name;
    private int age;
    private String idCard;
    private String school;
    private String job;
    private String remark;
    private Date createDate;
    private Date modifyDate;
    private int deleteFlag;
}

5、新增UserMapper.java

package cn.scp.mybatis.native_mybatis.mapper;

import cn.scp.mybatis.native_mybatis.domain.UserDomain;

/**
 * @description
 * @date 2019/3/17 10:30
 */
public interface UserMapper {
    public UserDomain selectById(int id);
}

6、新增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="cn.scp.mybatis.native_mybatis.mapper.UserMapper">
    <select id="selectById" resultType="cn.scp.mybatis.native_mybatis.domain.UserDomain">
        select * from t_user_info where id=#{id}
    </select>
</mapper>

7、新增mybatis工具类MybatisUtil.java

package cn.scp.mybatis.native_mybatis.util;

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 java.io.Reader;

public class MybatisUtil {
    public static SqlSessionFactory sessionFactory;
    static {
        try{
            Reader reader = Resources.getResourceAsReader("mybatis.xml");
            sessionFactory = new SqlSessionFactoryBuilder().build(reader);
        }
        catch (Exception e){
            System.out.println(e);
        }
    }

    public static SqlSession getSession(){
        return sessionFactory.openSession();
    }
}

8、测试

package cn.scp.mybatis.native_mybatis;

import cn.scp.mybatis.native_mybatis.domain.UserDomain;
import cn.scp.mybatis.native_mybatis.mapper.UserMapper;
import cn.scp.mybatis.native_mybatis.util.MybatisUtil;
import org.apache.ibatis.session.SqlSession;

public class Main {
    public static void main(String[] args) {
        SqlSession session = MybatisUtil.getSession();
        UserMapper userMapper = session.getMapper(UserMapper.class);
        try{
            UserDomain userDomain = userMapper.selectById(8);
            System.out.println(userDomain);
        }
        catch (Exception e){
            System.out.println(e);
        }
    }
}

结果:

UserDomain(id=8, name=7, age=11, idCard=null, school=鱼小学, job=null, remark=null, createDate=null, modifyDate=null, deleteFlag=0)

 

mybatis简单示例

标签:new   factory   turn   char   builder   .sql   out   epo   jdbc   

原文地址:https://www.cnblogs.com/suchunping/p/10540172.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!