SpringBoot - 整合Mybatis+Druid+数据库(注解版)

不羁的心 提交于 2019-12-02 03:23:12

运行展示


正题
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=20
spring.datasource.maxActive=50
spring.datasource.minIdle=10
#spring.datasource.maxWait=60000
#spring.datasource.minEvictableIdleTimeMillis=3600000
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

注解的形式,小编感觉比XML好用

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是写接口
————————————————
版权声明:本文为CSDN博主「沐荔」的原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接及本声明。
原文链接:https://blog.csdn.net/qq_41920732/article/details/85936063

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!