一起学习Spring boot 2.1.X | 第五篇:Mybatis Druid 数据库(注解版)

运行展示

正题

Spring boot :2.1.5RELEASE ;数据库(Mysql、Oracle);Mybatis;阿里云的连接池 : Druid ;

步骤

1.POM依赖

<!-- MyBatis -->
<dependency>
    <groupId>org.mybatis.spring.boot</groupId>
    <artifactId>mybatis-spring-boot-starter</artifactId>
    <version>1.3.2</version>
</dependency>

<!-- MySQL -->
<dependency>
    <groupId>mysql</groupId>
    <artifactId>mysql-connector-java</artifactId>
</dependency>

<!-- Oracle -->
<!--<dependency>
	<groupId>com.oracle</groupId>
	<artifactId>ojdbc14</artifactId>
	<version>10.2.0.4.0</version>
/dependency>-->

<!-- Druid -->
<dependency>
    <groupId>com.alibaba</groupId>
    <artifactId>druid</artifactId>
    <version>1.1.16</version>
</dependency>

 

2.application.properties

#Mybatis+MySQL+Druid
#Mysql时区异常URL后添加serverTimezone=GMT%2B8/连接池:typr/初始化连接:initialSize/最大空闲数:maxActive/最小空闲数:minIdle/获取连接等待时间:maxWait/最小等待时间:minEvictableIdleTimeMillis/关闭后不自动提交:defaultAutoCommit
spring.datasource.type=com.alibaba.druid.pool.DruidDataSource
spring.datasource.url=jdbc:mysql://127.0.0.1:3306/test?useUnicode=true&serverTimezone=GMT%2B8&characterEncoding=utf-8&allowMultiQueries=true&useSSL=false
#spring.datasource.url=jdbc:oracle:thin:@127.0.0.1:1521:test
spring.datasource.username=root
spring.datasource.password=root
spring.datasource.driverClassName=com.mysql.cj.jdbc.Driver

# 初始化大小,最小,最大
spring.datasource.initialSize=5
spring.datasource.minIdle=5
spring.datasource.maxActive=20
# 配置获取连接等待超时的时间
spring.datasource.maxWait=60000
# 配置间隔多久才进行一次检测,检测需要关闭的空闲连接,单位是毫秒
spring.datasource.timeBetweenEvictionRunsMillis=60000
# 配置一个连接在池中最小生存的时间,单位是毫秒
spring.datasource.minEvictableIdleTimeMillis=300000
spring.datasource.validationQuery=SELECT 1 FROM DUAL
spring.datasource.testWhileIdle=true
spring.datasource.testOnBorrow=false
spring.datasource.testOnReturn=false
# 打开PSCache,并且指定每个连接上PSCache的大小
spring.datasource.poolPreparedStatements=true
spring.datasource.maxPoolPreparedStatementPerConnectionSize=20
# 配置监控统计拦截的filters,去掉后监控界面sql无法统计,'wall'用于防火墙
spring.datasource.filters=stat,wall,log4j
# 通过connectProperties属性来打开mergeSql功能;慢SQL记录
spring.datasource.connectionProperties=druid.stat.mergeSql=true;druid.stat.slowSqlMillis=5000
# 合并多个DruidDataSource的监控数据
#spring.datasource.useGlobalDataSourceStat=true

spring.datasource.defaultAutoCommit=false

 

3.其他文件生成

User.java

public class User {

    public Integer uid;
    public String uname;
    public String upassword;

    public Integer getUid() {
        return uid;
    }

    public void setUid(Integer uid) {
        this.uid = uid;
    }

    public String getUanme() {
        return uname;
    }

    public void setUanme(String uanme) {
        this.uname = uanme;
    }

    public String getUpassword() {
        return upassword;
    }

    public void setUpassword(String upassword) {
        this.upassword = upassword;
    }

    @Override
    public String toString() {
        return "User{" +
                "uid=" + uid +
                ", uname='" + uname + '\'' +
                ", upassword='" + upassword + '\'' +
                '}';
    }
}

UserDao.java

import com.spring.boot.bean.User;
import org.apache.ibatis.annotations.Select;
import org.apache.ibatis.annotations.Update;

import java.util.List;

@Mapper
public interface UserDao {

    @Select("select * from user")
    public List<User> AllUser();

    @Update("<script> " + "update user" +
            "<set>"+  "<if test='uname!=null'>uname=#{uname},</if>"+
            "<if test='upassword!=null'>upassword=#{upassword},</if>"+
            "</set>"+ "where uid=#{uid}"+
            " </script> ")
    public int Update(User user);

}

UserService.java

import com.spring.boot.bean.User;
import java.util.List;

public interface UserService {

    public List<User> AllUser();

    public int Update(User user);

}

 

UserImpl.java
主要是注解问题Service可以命名,主要还是看自己的日常使用

import com.spring.boot.bean.User;
import com.spring.boot.dao.UserDao;
import com.spring.boot.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class UserImpl implements UserService {

    @Autowired
    private UserDao userDao;

    @Override
    public List<User> AllUser() {

        return userDao.AllUser();
    }

    @Override
    public int Update(User user) {
        return userDao.Update(user);
    }
}

UserController.java

 

import com.spring.boot.bean.User;
import com.spring.boot.service.impl.UserImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import javax.servlet.http.HttpServletRequest;

@RestController
public class UserController {

    @Autowired
    private  HttpServletRequest request;

    @Autowired
    private UserImpl userimpl;

     @RequestMapping("/api/login")
    public String Login(User user) {

        HttpSession session = request.getSession();

        //存入Session
        //session.setAttribute("user", user);
        //单位为秒,设置为-1时不再失效
        //session.setMaxInactiveInterval(60 * 60 * 24 * 30);
        //注销登陆
        //request.getSession().removeAttribute("user");

        return userimpl.UserAll(user).toString();
    }
}

 

注:userDao报红解决方法

 

注:启动类加入注解@MapperScan("路径)
小编这是Java web项目,@Controller注解是界面、@RestController是写接口

评论 130
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值