Docker裡面沒有你期望的命令、甚至沒有yum怎麼辦?
阿新 • • 發佈:2021-10-07
SpringBoot整合MyBatis
系統要求
Java 8+
springBoot2.5 +
建立springBoot專案工程
匯入依賴
<dependencies> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-devtools</artifactId> <scope>runtime</scope> <optional>true</optional> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-jdbc</artifactId> </dependency> <dependency> <groupId>com.alibaba</groupId> <artifactId>druid</artifactId> <version>1.2.6</version> </dependency> <dependency> <groupId>log4j</groupId> <artifactId>log4j</artifactId> <version>1.2.17</version> </dependency> <dependency> <groupId>mysql</groupId> <artifactId>mysql-connector-java</artifactId> <scope>runtime</scope> </dependency> <!--Druid starter--> <dependency> <groupId>com.alibaba</groupId> <artifactId>druid-spring-boot-starter</artifactId> <version>1.1.22</version> </dependency> <dependency> <groupId>org.mybatis.spring.boot</groupId> <artifactId>mybatis-spring-boot-starter</artifactId> <version>2.2.0</version> </dependency> <dependency> <groupId>org.projectlombok</groupId> <artifactId>lombok</artifactId> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-test</artifactId> <scope>test</scope> </dependency> </dependencies>
專案結構目錄
配置application.yaml
spring: datasource: driver-class-name: com.mysql.cj.jdbc.Driver url: jdbc:mysql://localhost:3307/webapp1?serverTimezone=UTC&useUnicode=true&characterEncoding=utf-8 username: webapp1 password: webapp1 type: com.alibaba.druid.pool.DruidDataSource #Spring Boot 預設是不注入這些屬性值的,需要自己繫結 #druid 資料來源專有配置 # 初始化大小,最小,最大 initialSize: 5 minIdle: 5 maxActive: 200 # 配置獲取連線等待超時的時間 maxWait: 60000 # 配置間隔多久才進行一次檢測,檢測需要關閉的空閒連線,單位是毫秒 timeBetweenEvictionRunsMillis: 60000 # 配置一個連線在池中最小生存的時間,單位是毫秒 minEvictableIdleTimeMillis: 300000 # 用來檢測連線是否有效的sql,要求是一個查詢語句 validationQuery: SELECT 1 FROM DUAL # 建議配置為true,不影響效能,並且保證安全性。申請連線的時候檢測,如果空閒時間大於timeBetweenEvictionRunsMillis,執行validationQuery檢測連線是否有效。 testWhileIdle: true # 申請連線時執行validationQuery檢測連線是否有效,做了這個配置會降低效能 testOnBorrow: false # 歸還連線時執行validationQuery檢測連線是否有效,做了這個配置會降低效能。 testOnReturn: false # 是否快取preparedStatement,也就是PSCache。PSCache對支援遊標的資料庫效能提升巨大,比如說oracle。在mysql下建議關閉。 poolPreparedStatements: true # 要啟用PSCache,必須配置大於0,當大於0時,poolPreparedStatements自動觸發修改為true。 max-pool-prepared-statement-per-connection-size: 50 #配置監控統計攔截的filters,stat:監控統計、log4j:日誌記錄、wall:防禦sql注入 #如果允許時報錯 java.lang.ClassNotFoundException: org.apache.log4j.Priority #則匯入 log4j 依賴即可,Maven 地址:https://mvnrepository.com/artifact/log4j/log4j filters: stat,wall,log4j # 合併多個DruidDataSource的監控資料 useGlobalDataSourceStat: true # 通過connectProperties屬性來開啟mergeSql功能;慢SQL記錄 connectionProperties: druid.stat.mergeSql=true;druid.stat.slowSqlMillis=500 mybatis: #sql對映檔案的位置 # mapper-locations: classpath:com/xiang/mapper/*.xml mapper-locations: classpath:mapper/*.xml #開啟駝峰命名轉化 configuration: map-underscore-to-camel-case: true #開啟別名 type-aliases-package: com.xiang
建立User類
package com.xiang.pojo; import lombok.AllArgsConstructor; import lombok.Data; import lombok.NoArgsConstructor; import org.springframework.stereotype.Component; import java.util.Date; /** * Created by IntelliJ IDEA. * User: xiang * Date: 2021/10/17 23:52 */ @Component @Data @NoArgsConstructor @AllArgsConstructor public class User { private int id; private String username; private String age; private Date birthday; }
建立UserMapper
package com.xiang.mapper;
import com.xiang.pojo.User;
import org.apache.ibatis.annotations.Mapper;
import org.mybatis.spring.annotation.MapperScan;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.stereotype.Repository;
import java.util.List;
/**
* Created by IntelliJ IDEA.
* User: xiang
* Date: 2021/10/17 23:54
*/
@Mapper
@Repository
public interface UserMapper {
User findById(int id);
List<User> findAll();
}
建立UserMapper.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.xiang.mapper.UserMapper">
<select id="findById" resultType="user">
select * from user where id = #{id}
</select>
<select id="findAll" resultType="user">
select * from user
</select>
</mapper>
建立UserService
package com.xiang.service;
import com.xiang.pojo.User;
import java.util.List;
/**
* Created by IntelliJ IDEA.
* User: xiang
* Date: 2021/10/18 15:01
*/
public interface UserService {
User findById(int id);
List<User> findAll();
}
建立UserServiceImpl
package com.xiang.service.impl;
import com.xiang.mapper.UserMapper;
import com.xiang.pojo.User;
import com.xiang.service.UserService;
import org.mybatis.spring.annotation.MapperScan;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.stereotype.Service;
import java.util.List;
/**
* Created by IntelliJ IDEA.
* User: xiang
* Date: 2021/10/18 15:02
*/
@Service
public class UserServiceImpl implements UserService {
@Autowired
private UserMapper userMapper;
@Override
public User findById(int id) {
return userMapper.findById(id);
}
@Override
public List<User> findAll() {
return userMapper.findAll();
}
}
執行測試
package com.xiang;
import com.alibaba.druid.pool.DruidDataSource;
import com.xiang.pojo.User;
import com.xiang.service.impl.UserServiceImpl;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.jdbc.core.JdbcTemplate;
import javax.sql.DataSource;
import java.sql.Connection;
import java.sql.SQLException;
import java.util.List;
import java.util.Map;
@SpringBootTest
class SpringBootDatabaseApplicationTests {
@Autowired
UserServiceImpl userService;
/***
* 根據id查
*/
@Test
public void findById(){
User user = userService.findById(1);
System.out.println(user);
}
/**
* 查所有
*/
@Test
public void findAll(){
System.out.println(userService.findAll());
System.out.println("/*******************************************/");
List<User> userList = userService.findAll();
for (User user : userList) {
System.out.println(user);
}
}
}
測試執行結果
建立UserController
package com.xiang.controller;
import com.xiang.pojo.User;
import com.xiang.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
/**
* Created by IntelliJ IDEA.
* User: xiang
* Date: 2021/10/18 22:03
*/
@Controller
public class UserController {
@Autowired
UserService userService;
@RequestMapping("findById")
@ResponseBody
public String findById(@RequestParam("id") int id) {
User byId = userService.findById(id);
return byId.toString();
}
}