mybatis简单示例

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)

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

时间: 2024-10-19 15:51:06

mybatis简单示例的相关文章

SpringMVC+myBatis简单示例分享

1. [代码]控制器片段 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 package com.wg.test; import javax.se

Mybatis的简单示例

首先新建一个JavaWeb项目并导入mybatis依赖的jar包,同时Mybatis是对数据库的操作所以我们需要在数据库中新建一个表user用来演示. 新建完表之后我们还需要建立相对应的实体类User.java并添加set和get方法: 1 public class User { 2 private String username; 3 private String password; 4 private int age; 5 public String getUsername() { 6 re

mybatis 简单使用示例(单独使用):

mybatis的单独使用简单示例: 步骤1: 新建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=

AMQP消息队列之RabbitMQ简单示例

前面一篇文章讲了如何快速搭建一个ActiveMQ的示例程序,ActiveMQ是JMS的实现,那这篇文章就再看下另外一种消息队列AMQP的代表实现RabbitMQ的简单示例吧.在具体讲解之前,先通过一个图来概览下: 1.添加Maven依赖 <!-- rabbitmq begin --> <dependency> <groupId>org.springframework.amqp</groupId> <artifactId>spring-rabbit

HMM的维特比算法简单示例

今天读了一位大牛的关于HMM的技术博客,读完之后,写了一个关于维特比算法的简单示例,用scala和java语言混合编写的.现在上传之. package com.txq.hmm import java.utilimport scala.collection._ /** * HMM维特比算法,根据显示状态链条估计隐式链条 * @param states 隐式states * @param observations 显式states * @param start_probability 初始概率向量

spring-servlet.xml简单示例

spring-servlet.xml简单示例 某个项目中的spring-servlet.xml 记下来以后研究用 1 <!-- springMVC简单配置 --> 2 <?xml version="1.0" encoding="UTF-8"?> 3 <beans xmlns="http://www.springframework.org/schema/beans" 4 xmlns:xsi="http://w

关于Ajax实现的简单示例

一.代码示例 关于Ajax的基本概念(包括XMLHttpRequest对象及其相关方法属性)移步这里(w3school中文版)学习了解. <!doctype html> <html lang = "en"> <head> <meta charset = "utf-8"> <title>使用Ajax异步加载数据</title> <script type = "text/javasc

【转】bind简单示例

bind简单示例代码 namespace { class placeholder_ {}; placeholder_ __1; } template <typename R, typename T, typename Arg> class simple_bind_t { private: typedef R (T::*F)(Arg); F f_; T* t_; Arg& a_; public: simple_bind_t(F f, T* t, Arg &a) : f_(f),

SQL左连接、右连接和内连接的简单示例

left join(左联接) 返回包括左表中的所有记录和右表中联结字段相等的记录: right join(右联接) 返回包括右表中的所有记录和左表中联结字段相等的记录: inner join(等值连接) 只返回两个表中联结字段相等的行:举例如下: -------------------------------------------- 表A记录如下: aID aNum 1 a20050111 2 a20050112 3 a20050113 4 a20050114 5 a20050115 表B记录