SpringBoot - 整合Mybatis+Druid+數據庫

正題
Spring boot :2.1.5RELEASE ;數據庫(Mysql、Oracle);Mybatis;阿里雲的鏈接池 : Druid ;java

步驟
1.POM依賴mysql

[Java] 純文本查看 複製代碼
?web

<font style="color:rgb(79, 79, 79)"><font face="&quot"><font style="font-size:16px"><!-- MyBatis -->/size/color<dependency>spring

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

</dependency>sql

<!-- MySQL -->
<dependency>數據庫

<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>

</dependency>apache

<!-- Oracle -->
<!--<dependency>api

<groupId>com.oracle</groupId>
<artifactId>ojdbc14</artifactId>
<version>10.2.0.4.0</version>

/dependency>-->session

<!-- Druid -->
<dependency>mybatis

<groupId>com.alibaba</groupId>
<artifactId>druid</artifactId>
<version>1.1.16</version>

</dependency>
</font></font></font>

2.application.properties

[Java] 純文本查看 複製代碼
?

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

[Java] 純文本查看 複製代碼
?

<font style="color:rgb(79, 79, 79)"><font face="&quot"><font style="font-size:16px">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 + '\'' +
            '}';
}

}
</font></font></font>

UserDao.java

註解的形式,小編感受比XML好用

[Java] 純文本查看 複製代碼
?

<font style="color:rgb(79, 79, 79)"><font face="&quot"><font style="font-size:16px">import com.spring.boot.bean.User;
import org.apache.ibatis.annotations.Select;
import org.apache.ibatis.annotations.Update;

import java.util.List;

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);

}
</font></font></font>
UserService.java

[Java] 純文本查看 複製代碼
?

<font style="color:rgb(79, 79, 79)"><font face="&quot"><font style="font-size:16px">import com.spring.boot.bean.User;
import java.util.List;

public interface UserService {

public List<User> AllUser();

public int Update(User user);

}
</font></font></font>
UserImpl.java
主要是註解問題Service能夠命名,主要仍是看本身的平常使用

[AppleScript] 純文本查看 複製代碼
?

<font style="color:rgb(79, 79, 79)"><font face="&quot"><font style="font-size:16px">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);
}

}

</font></font></font>
UserController.java

[Java] 純文本查看 複製代碼
?

<font style="color:rgb(79, 79, 79)"><font face="&quot"><font style="font-size:16px">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();
}

}
</font></font></font>
注:userDao報紅解決方法

注:啓動類加入註解@MapperScan("路徑)
小編這是Java web項目,@Controller註解是界面、@RestController是寫接口

做者:沐荔

來源:CSDN

原文:https://blog.csdn.net/qq_4192...

版權聲明:本文爲博主原創文章,轉載請附上博文連接!

FireShot Capture 001 - SpringBoot - 整合Mybatis+Druid+數據庫(註解版) - qq_419207.png (617.31 KB, 下載次數: 0)

FireShot Capture 001 - SpringBoot - 整合Mybatis+Druid+數據庫(註解版) - qq_419207.png
FireShot Capture 001 - SpringBoot - 整合Mybatis+Druid+數據庫(註解版) - qq_419207.png (617.31 KB, 下載次數: 0)

FireShot Capture 001 - SpringBoot - 整合Mybatis+Druid+數據庫(註解版) - qq_419207.png

相關文章
相關標籤/搜索