码迷,mamicode.com
首页 > 编程语言 > 详细

springboot整合mybatis

时间:2020-05-17 01:30:41      阅读:78      评论:0      收藏:0      [点我收藏+]

标签:serial   val   void   engine   rac   har   bind   serve   inf   

pom文件

<?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 https://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.3.0.RELEASE</version>
        <relativePath/> <!-- lookup parent from repository -->
    </parent>
    <groupId>com.blb</groupId>
    <artifactId>springboot_study</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <name>springboot_study</name>
    <description>Demo project for Spring Boot</description>
    <properties>
        <java.version>1.8</java.version>
    </properties>
    <dependencies>
        <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>1.3.2</version>
        </dependency>
        <dependency>
            <groupId>org.projectlombok</groupId>
            <artifactId>lombok</artifactId>
            <optional>true</optional>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
            <exclusions>
                <exclusion>
                    <groupId>org.junit.vintage</groupId>
                    <artifactId>junit-vintage-engine</artifactId>
                </exclusion>
            </exclusions>
        </dependency>
        <dependency>
            <groupId>commons-dbcp</groupId>
            <artifactId>commons-dbcp</artifactId>
            <version>1.4</version>
        </dependency>
        <dependency>
            <groupId>mysql</groupId>
            <artifactId>mysql-connector-java</artifactId>
            <version>5.1.47</version>
        </dependency>
    </dependencies>
    <build>
        <resources>
            <resource>
                <directory>src/main/java</directory>
                <includes>
                    <include>com/blb/mapper/*.xml</include>
                </includes>
            </resource>
        </resources>
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
            </plugin>
        </plugins>
    </build>
</project>

yml文件

spring:
  datasource:
    driver-class-name: com.mysql.jdbc.Driver
    url: jdbc:mysql://localhost:3306/boot?characterEncoding=utf8&serverTimezone=UTC
    type: org.apache.commons.dbcp.BasicDataSource
    username: root
    password: sugar888
mybatis:
  configuration:
    log-impl: org.apache.ibatis.logging.stdout.StdOutImpl
  mapper-locations: classpath:**/*.xml
  type-aliases-package: com.blb.dto.UserRole

dao  UserRoleDao

package com.blb.dto;
import java.io.Serializable;

public class UserRole implements Serializable {
    private static final long serialVersionUID = 1L;

    private Integer id;

    public static long getSerialVersionUID() {
        return serialVersionUID;
    }

    public Integer getId() {
        return id;
    }

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

    public String getRole() {
        return role;
    }

    public void setRole(String role) {
        this.role = role;
    }

    public String getUserName() {
        return userName;
    }

    public void setUserName(String userName) {
        this.userName = userName;
    }

    public String getPhone() {
        return phone;
    }

    public void setPhone(String phone) {
        this.phone = phone;
    }

    private String role;

    private String userName;

    private String phone;

}

mapper 接口

package com.blb.mapper;

import com.blb.dto.UserRole;
import org.apache.ibatis.annotations.Mapper;

import java.util.List;

@Mapper
public interface UserRoleInfoMapper {
    int saveTest(UserRole userRole);

    List<UserRole> selectAll();

    int update(Integer id,String phone);

    int delete(Integer id);

}

mapper.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.blb.mapper.UserRoleInfoMapper">
    <resultMap id="BaseResultMap" type="com.blb.dto.UserRole">
        <id property="id" column="id"></id>
        <result column="role" jdbcType="VARCHAR" property="role"></result>
        <result column="userName" jdbcType="VARCHAR" property="userName"></result>
        <result column="phone" jdbcType="VARCHAR" property="phone"></result>
    </resultMap>

    <select id="selectAll" resultMap="BaseResultMap">
        SELECT * FROM user_role
    </select>

    <insert id="saveTest" useGeneratedKeys="true" keyProperty="id">
        INSERT INTO user_role(phone,userName,role) values (#{phone},#{userName},#{role})
    </insert>

    <update id="update">
        update user_role set phone = #{param2} where id = #{param1}
    </update>


    <delete id="delete" parameterType="java.lang.Integer">
        delete from user_role where id = #{id}
    </delete>

</mapper>

service

package com.blb.service;

import com.blb.dto.UserRole;
import com.blb.mapper.UserRoleInfoMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class UserRoleService {
    @Autowired
    private UserRoleInfoMapper userRoleInfoMapper;

    public List<UserRole> selectAll()
    {
        return userRoleInfoMapper.selectAll();
    }

    public int saveTest(UserRole userRole)
    {
        return userRoleInfoMapper.saveTest(userRole);
    }

    public int update(Integer id,String phone)
    {
        return userRoleInfoMapper.update(id,phone);
    }

    public int delete(Integer id)
    {
        return userRoleInfoMapper.delete(id);
    }

}

controller

package com.blb.controller;

import com.blb.dto.UserRole;
import com.blb.service.UserRoleService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

@RestController
@RequestMapping("/db")
public class MybatisRest {
    @Autowired
    private UserRoleService userRoleService;

    @RequestMapping("/query")
    public List<UserRole> query()
    {
        return userRoleService.selectAll();
    }

    @RequestMapping("/save")
    public Object save()
    {
        UserRole userRole = new UserRole();
        userRole.setRole("test");
        userRole.setUserName("test");
        userRole.setPhone("123456789");
        return userRoleService.saveTest(userRole);
    }

    @RequestMapping("/update")
    public Object update(){
        return userRoleService.update(4,"454");
    }

    @RequestMapping("delete")
    public Object delete()
    {
        return userRoleService.delete(4);
    }

}

 

springboot整合mybatis

标签:serial   val   void   engine   rac   har   bind   serve   inf   

原文地址:https://www.cnblogs.com/Tsugar/p/12902977.html

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