• SpringBoot整合Mybatis


    1.用idea构建项目
    在这里插入图片描述
    2.使用Druid连接池

    <!--druid连接池-->
            <dependency>
                <groupId>com.alibaba</groupId>
                <artifactId>druid</artifactId>
                <version>1.1.0</version>
            </dependency>
    

    3.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>
        <parent>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-parent</artifactId>
            <version>2.1.3.RELEASE</version>
            <relativePath/> <!-- lookup parent from repository -->
        </parent>
        <groupId>com.yd</groupId>
        <artifactId>springboot-mybatis</artifactId>
        <version>0.0.1-SNAPSHOT</version>
        <name>springboot-mybatis</name>
        <description>Demo project for Spring Boot</description>
    
        <properties>
            <java.version>1.8</java.version>
        </properties>
    
        <dependencies>
            <dependency>
                <groupId>mysql</groupId>
                <artifactId>mysql-connector-java</artifactId>
            </dependency>
            <dependency>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-starter-jdbc</artifactId>
            </dependency>
            <dependency>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-starter-web</artifactId>
            </dependency>
            <dependency>
                <groupId>org.mybatis.spring.boot</groupId>
                <artifactId>mybatis-spring-boot-starter</artifactId>
                <version>2.0.0</version>
            </dependency>
         <!--druid连接池-->
            <dependency>
                <groupId>com.alibaba</groupId>
                <artifactId>druid</artifactId>
                <version>1.1.0</version>
            </dependency>
    
            <dependency>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-starter-test</artifactId>
                <scope>test</scope>
            </dependency>
        </dependencies>
    
        <build>
            <plugins>
                <plugin>
                    <groupId>org.springframework.boot</groupId>
                    <artifactId>spring-boot-maven-plugin</artifactId>
                </plugin>
                <!--在idea中使用Mybatis-generator插件快速生成代码-->
                <plugin>
                    <!--Mybatis-generator插件,用于自动生成Mapper和POJO-->
                    <groupId>org.mybatis.generator</groupId>
                    <artifactId>mybatis-generator-maven-plugin</artifactId>
                    <version>1.3.2</version>
                    <configuration>
                        <!--配置文件的位置-->
                        <configurationFile>src/main/resources/mybatis-generator-config.xml</configurationFile>
                        <verbose>true</verbose>
                        <overwrite>true</overwrite>
                    </configuration>
                    <executions>
                        <execution>
                            <id>Generate MyBatis Artifacts</id>
                            <goals>
                                <goal>generate</goal>
                            </goals>
                        </execution>
                    </executions>
                    <dependencies>
                        <dependency>
                            <groupId>org.mybatis.generator</groupId>
                            <artifactId>mybatis-generator-core</artifactId>
                            <version>1.3.2</version>
                        </dependency>
                    </dependencies>
                </plugin>
                <!--end-->
            </plugins>
        </build>
    
    </project>
    
    

    4.application.yml文件

    server:
      port: 9099
    
    #开发配置
    spring:
      profiles: dev
      datasource:
        # 使用druid数据源
        type: com.alibaba.druid.pool.DruidDataSource
        driver-class-name: com.mysql.jdbc.Driver
        url: jdbc:mysql://127.0.0.1:3306/test?serverTimezone=GMT%2B8
        username: root
        password: 123456
    ---
    #默认使用配置
    spring:
      profiles:
        active: dev
    mybatis:
      mapUnderscoreToCamelCase: true
      typeAliasesPackage: com.ls.pojo
      mapperLocations: classpath:mappers/*.xml
    
    logging:
      level:
        com.yd.mapper: debug
    
    

    5.使用MyBatis Generator自动生成代码自动生成代码,这里就不说了,可以看MyBatis Generator自动生成代码
    6.启动

    package com.yd;
    
    import org.mybatis.spring.annotation.MapperScan;
    import org.springframework.boot.SpringApplication;
    import org.springframework.boot.autoconfigure.SpringBootApplication;
    
    @SpringBootApplication
    //这里是扫描dao接口的包用于识别mybatis
    @MapperScan(basePackages="com.yd.mapper")
    public class SpringbootMybatisApplication {
    
        public static void main(String[] args) {
            SpringApplication.run(SpringbootMybatisApplication.class, args);
        }
    
    }
    
    

    7.pojo

    package com.yd.pojo;
    
    import java.io.Serializable;
    
    public class UserTab implements Serializable {
        private static final long serialVersionUID = -2143595433523717391L;
        private Integer id;
    
        private String name;
    
        private Integer age;
    
        private String sex;
    
        public Integer getId() {
            return id;
        }
    
        public void setId(Integer id) {
            this.id = id;
        }
    
        public String getName() {
            return name;
        }
    
        public void setName(String name) {
            this.name = name == null ? null : name.trim();
        }
    
        public Integer getAge() {
            return age;
        }
    
        public void setAge(Integer age) {
            this.age = age;
        }
    
        public String getSex() {
            return sex;
        }
    
        public void setSex(String sex) {
            this.sex = sex == null ? null : sex.trim();
        }
    }
    

    8.Mapper接口

    package com.yd.mapper;
    
    import com.yd.pojo.UserTab;
    
    import java.util.List;
    
    public interface UserTabMapper {
        List<UserTab> selectAll();
        int deleteByPrimaryKey(Integer id);
    
        int insert(UserTab record);
    
        int insertSelective(UserTab record);
    
        UserTab selectByPrimaryKey(Integer id);
    
        int updateByPrimaryKeySelective(UserTab record);
    
        int updateByPrimaryKey(UserTab record);
    }
    

    9.service接口

    package com.yd.service;
    
    
    
    import com.yd.pojo.UserTab;
    
    import java.util.List;
    
    public interface UserService {
    
        List<UserTab> findAll();
    
    }
    
    

    10.service实现类

    package com.yd.service.impl;
    
    
    import com.yd.mapper.UserTabMapper;
    import com.yd.pojo.UserTab;
    import com.yd.service.UserService;
    import org.springframework.beans.factory.annotation.Autowired;
    import org.springframework.stereotype.Service;
    
    import java.util.List;
    
    @Service
    public class UserServiceImpl implements UserService {
    
        @Autowired
        private UserTabMapper userTabMapper;
        @Override
        public List<UserTab> findAll() {
            List<UserTab> users = userTabMapper.selectAll();
            return  users;
    
        }
    }
    
    

    11.Controller

    package com.yd.controller;
    
    import com.yd.pojo.UserTab;
    import com.yd.service.UserService;
    import org.springframework.beans.factory.annotation.Autowired;
    import org.springframework.web.bind.annotation.GetMapping;
    import org.springframework.web.bind.annotation.RequestMapping;
    import org.springframework.web.bind.annotation.RestController;
    
    import java.util.List;
    
    
    @RestController
    @RequestMapping("/user")
    public class UserController {
    
        @Autowired
        private UserService userService;
    
        @GetMapping("/find")
        public List<UserTab>  find(){
            List<UserTab> all = userService.findAll();
            return all;
        }
    
    
    }
    
    

    12.UserTabMapper.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="com.yd.mapper.UserTabMapper" >
      <resultMap id="BaseResultMap" type="com.yd.pojo.UserTab" >
        <id column="id" property="id" jdbcType="INTEGER" />
        <result column="name" property="name" jdbcType="CHAR" />
        <result column="age" property="age" jdbcType="INTEGER" />
        <result column="sex" property="sex" jdbcType="CHAR" />
      </resultMap>
      <sql id="Base_Column_List" >
        id, name, age, sex
      </sql>
      <select id="selectByPrimaryKey" resultMap="BaseResultMap" parameterType="java.lang.Integer" >
        select 
        <include refid="Base_Column_List" />
        from user_tab
        where id = #{id,jdbcType=INTEGER}
      </select>
      <delete id="deleteByPrimaryKey" parameterType="java.lang.Integer" >
        delete from user_tab
        where id = #{id,jdbcType=INTEGER}
      </delete>
      <insert id="insert" parameterType="com.yd.pojo.UserTab" >
        insert into user_tab (id, name, age, sex
          )
        values (#{id,jdbcType=INTEGER}, #{name,jdbcType=CHAR}, #{age,jdbcType=INTEGER}, #{sex,jdbcType=CHAR}
          )
      </insert>
      <insert id="insertSelective" parameterType="com.yd.pojo.UserTab" >
        insert into user_tab
        <trim prefix="(" suffix=")" suffixOverrides="," >
          <if test="id != null" >
            id,
          </if>
          <if test="name != null" >
            name,
          </if>
          <if test="age != null" >
            age,
          </if>
          <if test="sex != null" >
            sex,
          </if>
        </trim>
        <trim prefix="values (" suffix=")" suffixOverrides="," >
          <if test="id != null" >
            #{id,jdbcType=INTEGER},
          </if>
          <if test="name != null" >
            #{name,jdbcType=CHAR},
          </if>
          <if test="age != null" >
            #{age,jdbcType=INTEGER},
          </if>
          <if test="sex != null" >
            #{sex,jdbcType=CHAR},
          </if>
        </trim>
      </insert>
      <update id="updateByPrimaryKeySelective" parameterType="com.yd.pojo.UserTab" >
        update user_tab
        <set >
          <if test="name != null" >
            name = #{name,jdbcType=CHAR},
          </if>
          <if test="age != null" >
            age = #{age,jdbcType=INTEGER},
          </if>
          <if test="sex != null" >
            sex = #{sex,jdbcType=CHAR},
          </if>
        </set>
        where id = #{id,jdbcType=INTEGER}
      </update>
      <update id="updateByPrimaryKey" parameterType="com.yd.pojo.UserTab" >
        update user_tab
        set name = #{name,jdbcType=CHAR},
          age = #{age,jdbcType=INTEGER},
          sex = #{sex,jdbcType=CHAR}
        where id = #{id,jdbcType=INTEGER}
      </update>
      <select id="selectAll" resultMap="BaseResultMap">
        select  * from  user_tab
      </select>
    </mapper>
    

    13.浏览器访问:http://localhost:9099/user/find
    在这里插入图片描述

  • 相关阅读:
    DES算法
    流密码_电子科大慕课笔记_七八讲
    王道考研《2019年操作系统考研复习指导》第一章笔记
    欧拉公式C++实现
    编译原理第一章学习笔记
    leetcode 728. Self Dividing Numbers
    leetcode 942. DI String Match
    2019年第十届蓝桥杯JAVA开发A组第二题
    2019年第十届蓝桥杯JAVA开发A组第一题
    python类的内置方法
  • 原文地址:https://www.cnblogs.com/szls-666/p/12494220.html
Copyright © 2020-2023  润新知