springboot mysql mybatis简单示例

springboot mysql mybatis简单示例

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.6.RELEASE</version>
        <relativePath/> <!-- lookup parent from repository -->
    </parent>
    <groupId>com.ll</groupId>
    <artifactId>jump</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <name>jump</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.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
        </dependency>

        <dependency>
            <groupId>mysql</groupId>
            <artifactId>mysql-connector-java</artifactId>
            <version>8.0.16</version>
        </dependency>

        <dependency>
            <groupId>org.apache.commons</groupId>
            <artifactId>commons-lang3</artifactId>
            <version>3.9</version>
        </dependency>
        <dependency>
            <groupId>org.mybatis.spring.boot</groupId>
            <artifactId>mybatis-spring-boot-starter</artifactId>
            <version>2.0.1</version>
        </dependency>

    </dependencies>

    <build>
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
            </plugin>
        </plugins>
    </build>

</project>

启动类

package com.ll.jump;

import org.mybatis.spring.annotation.MapperScan;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;

@SpringBootApplication
@MapperScan("com.ll.jump.mapper")
//@ImportResource(locations = {"classpath:META-INF/ctx/ctx_main.xml"})
public class JumpApplication {

    public static void main(String[] args) {
        SpringApplication.run(JumpApplication.class, args);
    }

}

mapper接口

package com.ll.jump.mapper;

import com.ll.jump.model.User;

import org.apache.ibatis.annotations.Mapper;


/**
 * 〈用户mapper〉
 *
 * @author 
 * @create 2019/7/12 0012
 */
@Mapper
public interface UserMapper {
    void insert(User user);
    User select(User user);
}

实体类

package com.ll.jump.model;

import org.apache.commons.lang3.builder.ToStringBuilder;

import java.io.Serializable;

/**
 * 〈用户对象〉
 *
 * @author 
 * @create 2019/7/12 0012
 */
public class User implements Serializable {
    private static final long serialVersionUID = -6910611766428801198L;

    private String id;
    private String name;

    public String getId() {
        return id;
    }

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

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    @Override
    public String toString(){
        return ToStringBuilder.reflectionToString(this);
    }
}

查询类

package com.ll.jump.repository;

import com.ll.jump.mapper.UserMapper;
import com.ll.jump.model.User;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

/**
 * 〈用户〉
 *
 * @author 
 * @create 2019/7/12 0012
 */
@Service
public class UserRepository {
    @Autowired
    private UserMapper userMapper;

    public void insert(User user) {
        userMapper.insert(user);
    }

    public User select(User user) {
        return userMapper.select(user);
    }
}

业务接口

package com.ll.jump.service;

import com.ll.jump.model.User;

public interface UserService {
    void insert(User user);
    User select(User user);
}

业务实现类

package com.ll.jump.service.impl;

import com.ll.jump.model.User;
import com.ll.jump.repository.UserRepository;
import com.ll.jump.service.UserService;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

/**
 * 〈〉
 *
 * @author 
 * @create 2019/7/12 0012
 */
@Service
public class UserServiceImpl implements UserService {
    @Autowired
    UserRepository userRepository;

    @Override
    public void insert(User user) {
        userRepository.insert(user);
    }

    @Override
    public User select(User user) {
        return userRepository.select(user);
    }
}

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.ll.jump.mapper.UserMapper">

    <!-- insert user -->
    <insert id = "insert" parameterType="com.ll.jump.model.User">
        INSERT INTO jump_user(id,name) VALUES(#{id},#{name});
    </insert>

    <!-- select user -->
    <select id="select" parameterType="com.ll.jump.model.User" resultType="com.ll.jump.model.User">
        SELECT id,name FROM jump_user WHERE id=#{id}
    </select>

</mapper>

配置文件

server.port=8888

spring.datasource.driver-class-name =com.mysql.jdbc.Driver

spring.datasource.url=jdbc:mysql://ip:3306/库名?useUnicode=true&characterEncoding=utf-8&autoReconnect=true&autoReconnectForPools=true&noAccessToProcedureBodies=true&useSSL=false
spring.datasource.username=用户名
spring.datasource.password=密码
#初始化时建立物理连接的个数。初始化发生在显示调用init方法,或者第一次getConnection时
druid.pool.initialSize=10
#最小连接池数量
druid.pool.minIdle=10
#最大连接池数量
druid.pool.maxActive=100
#获取连接时最大等待时间,单位毫秒。配置了maxWait之后,缺省启用公平锁,并发效率会有所下降,如果需要可以通过配置useUnfairLock属性为true使用非公平锁
druid.pool.maxWait=10000
#有两个含义: 1) Destroy线程会检测连接的间隔时间2) testWhileIdle的判断依据,详细看testWhileIdle属性的说明
druid.pool.timeBetweenEvictionRunsMillis=60000
#连接保持空闲而不被驱逐的最长时间
druid.pool.minEvictableIdleTimeMillis=300000
#建议配置为true,不影响性能,并且保证安全性。申请连接的时候检测,如果空闲时间大于timeBetweenEvictionRunsMillis,执行validationQuery检测连接是否有效
druid.pool.testWhileIdle=true
#申请连接时执行validationQuery检测连接是否有效,做了这个配置会降低性能
druid.pool.testOnBorrow=false
#归还连接时执行validationQuery检测连接是否有效,做了这个配置会降低性能
druid.pool.testOnReturn=false
#连接池创建的连接的默认的auto-commit状态
druid.pool.defaultAutoCommit=true

mybatis.typeAliasesPackage=com.ll.jump.model
mybatis.mapperLocations=classpath:/sqlmap/*.xml 

controller 类

package com.ll.jump.controller;

import com.ll.jump.model.User;
import com.ll.jump.service.UserService;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.servlet.ModelAndView;

/**
 * 〈测试〉
 *
 * @author 
 * @create 2019/7/10 0010
 */
@RestController
@RequestMapping(value = "test")
public class TestController {
    @Autowired
    UserService userService;

    @RequestMapping(value = "hi", method = RequestMethod.GET)
    public ModelAndView test(){
        ModelAndView modelAndView = new ModelAndView("/html/index.html");
        return modelAndView;
    }

    @RequestMapping(value = "user/insert", method = RequestMethod.POST)
    public void insertUser(@RequestBody User user){
        userService.insert(user);
    }

    @RequestMapping(value = "user/select", method = RequestMethod.POST)
    public User selectUser(@RequestBody User user){
        return userService.select(user);
    }
}

项目总体结构

 

调用测试

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值