zoukankan      html  css  js  c++  java
  • SpringBoot:第二篇 集成mybatis

    1.添加依赖

    <dependency>
        <groupId>org.mybatis.spring.boot</groupId>
        <artifactId>mybatis-spring-boot-starter</artifactId>
        <version>2.0.0</version>
    </dependency>
    <dependency>
        <groupId>mysql</groupId>
        <artifactId>mysql-connector-java</artifactId>
    </dependency>

    2.sql

    CREATE TABLE `t_student` (
      `id` int(11) NOT NULL AUTO_INCREMENT COMMENT '主建',
      `name` varchar(200) DEFAULT NULL COMMENT '姓名',
      `create_time` datetime DEFAULT NULL COMMENT '时间',
      PRIMARY KEY (`id`)
    ) ENGINE=InnoDB DEFAULT CHARSET=utf8 COMMENT '学生表';

    3.application.properties

    mybatis.mapper-locations=classpath:mappers/*.xml
    mybatis.type-aliases-package=com.example.demo.model
    spring.datasource.url = jdbc:mysql://db4free.net:3306/dbs_xq
    spring.datasource.username = root_xq
    spring.datasource.password = root123456
    spring.datasource.driverClassName=com.mysql.cj.jdbc.Driver

    4.类

    (1)dao

    package com.example.demo.dao;
    
    
    import com.example.demo.model.StudentEbo;
    import org.apache.ibatis.annotations.Delete;
    import org.apache.ibatis.annotations.Mapper;
    import org.apache.ibatis.annotations.Param;
    import org.apache.ibatis.annotations.Select;
    
    import java.util.List;
    
    /**
     * Package: com.vince.dao
     * User: 诸葛子房
     * Email: xiaoiqu2017wy@163.com
     * Date: 2019/3/21
     * Time: 11:36
     * Description:
     */
    @Mapper
    public interface StudentDao {
    
        @Select("select id,name,create_time as createTime from t_student")
        List<StudentEbo> findAll();
    
        List<StudentEbo> listStu(@Param("name") String name);
    
        StudentEbo getStuById(@Param("id") int id);
    
        void addStu(StudentEbo stu);
    
        void insertByBatch(List<StudentEbo> studentEbos);
    
        @Delete("delete from t_student")
        void delete();
    
    }
    

    (2)model

    package com.example.demo.model;
    
    import com.fasterxml.jackson.annotation.JsonFormat;
    
    import java.util.Date;
    
    /**
     * Package: com.vince.model
     * User: 诸葛子房
     * Email: xiaoiqu2017wy@163.com
     * Date: 2019/3/21
     * Time: 11:35
     * Description:
     */
    public class StudentEbo {
    
        private int id;
        private String name;
    
        @JsonFormat(pattern="yyyy-MM-dd HH:mm:ss",timezone="GMT+8")
        private Date createTime;
    
        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 Date getCreateTime() {
            return createTime;
        }
    
        public void setCreateTime(Date createTime) {
            this.createTime = createTime;
        }
    }
    

    (3)启动类----@MapperScan("com.example.demo.dao")

    package com.example.demo;
    
    import org.mybatis.spring.annotation.MapperScan;
    import org.springframework.boot.SpringApplication;
    import org.springframework.boot.autoconfigure.SpringBootApplication;
    
    @MapperScan("com.example.demo.dao")
    @SpringBootApplication
    public class DemoApplication {
    
        public static void main(String[] args) {
            SpringApplication.run(DemoApplication.class, args);
        }
    }
    

    (4)mapper 文件

    <?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.example.demo.dao.StudentDao">
    
        <resultMap type="com.example.demo.model.StudentEbo" id="StudentMap">
            <result property="id" column="id" jdbcType="INTEGER" javaType="Integer"/>
            <result property="name" column="name" jdbcType="VARCHAR" javaType="String"/>
            <result property="createTime" column="create_time" jdbcType="TIMESTAMP" javaType="java.util.Date"/>
        </resultMap>
    
        <sql id="studentEboMap">
            SELECT
            s.id,s.name,s.create_time
            FROM
            t_student AS s
        </sql>
    
        <select id="getStuById"
                resultMap="StudentMap">
            SELECT
            s.id,s.name,s.create_time
            FROM
            t_student AS s where id=#{id}
        </select>
    
        <select id="listStu" resultMap="StudentMap">
            <include refid="studentEboMap"/>
            <where>
                1=1
                <if test=" name != null and name != '' ">
                    AND s.name LIKE '%${name}%'
                </if>
            </where>
        </select>
    
    
        <insert id="insertByBatch">
            INSERT INTO t_student
            (id, name, create_time)
            VALUES
            <foreach collection ="list" item="user" separator =",">
                (#{user.id}, #{user.name}, #{user.createTime})
            </foreach >
        </insert>
    
    
    </mapper>

    5.使用

    package com.example.demo.controller;
    
    /**
     * @author xiaoqiu
     * @Date 2019-06-14 15:05
     **/
    
    import com.example.demo.dao.StudentDao;
    import lombok.extern.slf4j.Slf4j;
    import org.springframework.beans.factory.annotation.Autowired;
    import org.springframework.stereotype.Controller;
    import org.springframework.web.bind.annotation.RequestMapping;
    import org.springframework.web.bind.annotation.ResponseBody;
    
    @Slf4j
    @Controller
    @RequestMapping("/")
    public class TestController {
    
        @Autowired
        private StudentDao studentDao;
    
        @RequestMapping("/")
        @ResponseBody
        public String index() {
            log.info("++++");
            return studentDao.getStuById(1).getName() + "hello world";
        }
    
    }
  • 相关阅读:
    【Linux】ubuntu各文件夹简介
    【Linux】 ubuntu 12.04 iNode Client找不到库libjpeg和libtiff的解决方法
    【Coding】ant 的 javac标签 (归纳)
    【Coding】Ant脚本命令
    【Linux】Ubuntu使用技巧
    【Linux】ubuntu下词典软件Goldendict介绍(可屏幕取词)和StarDict(星际译王)的安装
    【Coding】Ubuntu/环境变量:修改/etc/environment 导致开机不能登录!
    备用访问映射
    开发Silverlight类型的WebPart部署到Sharepoint2010上(转)
    (转)通过Internet访问 SharePoint
  • 原文地址:https://www.cnblogs.com/zgzf/p/11028043.html
Copyright © 2011-2022 走看看