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

一起学习Spring boot 2.1.X | 第五篇:Mybatis Druid 数据库(注解版)「建议收藏」运行展示正题Springboot:2.1.5RELEASE;数据库(Mysql、Oracle);Mybatis;阿里云的连接池:Druid;步骤1.POM依赖<!–MyBatis–><dependency><groupId>org.mybatis.spring.boot</groupId>…

大家好,又见面了,我是你们的朋友全栈君。

运行展示

一起学习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报红解决方法一起学习Spring boot 2.1.X | 第五篇:Mybatis Druid 数据库(注解版)「建议收藏」

 

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

版权声明:本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请联系我们举报,一经查实,本站将立刻删除。

发布者:全栈程序员-站长,转载请注明出处:https://javaforall.net/163542.html原文链接:https://javaforall.net

(0)
上一篇 2022年7月23日 下午1:00
下一篇 2022年7月23日 下午1:16


相关推荐

  • OpenClaw 200% 活用术 — 实战高级用例合集 🚀

    OpenClaw 200% 活用术 — 实战高级用例合集 🚀

    2026年3月13日
    2
  • idea2021.7激活码【永久激活】「建议收藏」

    (idea2021.7激活码)2021最新分享一个能用的的激活码出来,希望能帮到需要激活的朋友。目前这个是能用的,但是用的人多了之后也会失效,会不定时更新的,大家持续关注此网站~IntelliJ2021最新激活注册码,破解教程可免费永久激活,亲测有效,下面是详细链接哦~https://javaforall.net/100143.html…

    2022年3月21日
    143
  • 基于单片机的毕业设计题目_单片机课题

    基于单片机的毕业设计题目_单片机课题地铁报站系统方案说明经过酷毕啦电子设计室3个月的奋斗和不限努力,设计出了2款有个报站系统的课题:一种为自动报站系统;一种为无线控制报站系统,该系统除了可以设计为地铁报站系统,还可以设计成公交报站系统,有需要了解和学习的同学可以加q:3407232510来进行学习和探讨。本系统所有的全国各地的地铁,无论几号线,都实用这套系统,完全个性定制!产品视频讲解视频链接*(以长沙地铁2号线讲解为例):htt…

    2026年4月17日
    4
  • 常用存储过程语法

    常用存储过程语法 前面学过了基本的存储过程,见 存储过程入门 现在学一下常用的存储过程的语法,只要花一点点时间学习下,就能用存储过程实现很复杂的功能,可以少写很多代码。 为了方便说明,数据库使用SQLServer的示例数据库,Northwind和pubs,如果SQLServer中没有的话,可以按下面的方法安装1,下载SQL2000SampleDb.msi,下载地址是:http://ww

    2022年7月17日
    20
  • 数据帧的学习整理

    数据帧的学习整理事先声明,本文档所有内容均在本人的学习和理解上整理,不具有权威性,甚至不具有准确性,本人也会在以后的学习中对不合理之处进行修改。在了解数据帧之前,我们得先知道OSI参考模型咱们从下往上数,数据帧在

    2022年8月5日
    7
  • 使用nginx配置二级域名

    使用nginx配置二级域名最近想把三个项目配在一个服务器上,于是想使用nginx配置二级域名实现。1.域名添加解析我的是阿里云的域名,所以首先给自己的域名添加解析。打算使用www.codeliu.com,test1.codeliu.com,test2.codeliu.com这三个域名,其中test1.codeliu.com,test2.codeliu.com作为二级域名。2.准备好三个项目ecl…

    2022年5月21日
    34

发表回复

您的邮箱地址不会被公开。 必填项已用 * 标注

关注全栈程序员社区公众号