Springboot整合MyBatis進行數據庫交互

  1. Mybatis訪問數據庫
    1. 註解方式
      1. 創建項目
        1. 勾選mysql Driver; JDBC API;Mybatis
      2. 配置數據源
        1. spring:
        2.   datasource:
        3.     username: root
        4.     password: yahang.521
        5.     driver-class-name: com.mysql.cj.jdbc.Driver
        6.    
        7.     # Hikari 連接池配置
        8.     hikari:
        9.       minimum-idle: 5 # 最小空閒連接數量    
        10.       idle-timeout: 180000 # 空閒連接存活最大時間,默認600000(10分鐘)    
        11.       maximum-pool-size: 10 # 連接池最大連接數,默認是10   
        12.       auto-commit: true  # 此屬性控制從池返回的連接的默認自動提交行爲,默認值:true    
        13.       pool-name: HikariPool # 連接池名稱     
        14.       max-lifetime: 1800000 # 此屬性控制池中連接的最長生命週期,值0表示無限生命週期,默認1800000即30分鐘    
        15.       connection-timeout: 30000 # 數據庫連接超時時間,默認30秒,即30000
        16.       connection-test-query: SELECT 1
      3. 創建操作不同表對應的Mapper接口
        1.      @Mapper
        2. public interface StudentMapper {
        3.    
        4.     @Select("select * from student where id=#{id}")
        5.     Student queryStudentById(Integer id);
        6.    
        7.     @Delete("delete from student where id = #{id}")
        8.     int deleteStudentById(Integer id);
        9.       
        10. }
      4. 編寫業務代碼
        1.      @Controller
        2. public class StController {
        3.    
        4.     @Autowired
        5.     private StudentMapper studentMapper;
        6.    
        7.     @ResponseBody
        8.     @RequestMapping("/getStudentById")
        9.     public Student getStudentById(Integer id) {
        10.         Student student = studentMapper.queryStudentById(id);
        11.         return student;
        12.     }
        13. }
      5. 其他
        1. 批量掃描包下的mapper類
        2. @MapperScan(value = "com.njupt.mapper")
        3. @SpringBootApplication
        4. public class MybatisApplication {
        5.      public static void main(String[] args) {
        6.            SpringApplication.run(MybatisApplication.class, args);
        7.      }
        8. }
    2. 配置文件方式
      1. 創建項目
        1. 勾選mysql Driver; JDBC API;Mybatis
      2. 配置數據源
        1. spring:
        2.   datasource:
        3.     username: root
        4.     password: yahang.521
        5.     driver-class-name: com.mysql.cj.jdbc.Driver
        6.    
        7.     # Hikari 連接池配置
        8.     hikari:
        9.       minimum-idle: 5 # 最小空閒連接數量    
        10.       idle-timeout: 180000 # 空閒連接存活最大時間,默認600000(10分鐘)    
        11.       maximum-pool-size: 10 # 連接池最大連接數,默認是10   
        12.       auto-commit: true  # 此屬性控制從池返回的連接的默認自動提交行爲,默認值:true    
        13.       pool-name: HikariPool # 連接池名稱     
        14.       max-lifetime: 1800000 # 此屬性控制池中連接的最長生命週期,值0表示無限生命週期,默認1800000即30分鐘    
        15.       connection-timeout: 30000 # 數據庫連接超時時間,默認30秒,即30000
        16.       connection-test-query: SELECT 1
      3. 在resources目錄下創建兩個目錄
        1. resources/mybatis/config:mybatis全局配置文件目錄
          1. 新建mybatis-config.xml文件: 該文件無需配置環境,也無需引用mapper文件,直接配置就可以
            1. <?xml version="1.0" encoding="UTF-8" ?>
            2. <!DOCTYPE configuration
            3.   PUBLIC "-//mybatis.org//DTD Config 3.0//EN"
            4. <configuration>
            5.     <settings>
            6.         <setting name="mapUnderscoreToCamelCase" value="true"/>
            7.     </settings> 
            8. </configuration>
        2. resources/mybatis/mapper:mybatis sql映射文件目錄
          1. 新建StudentMapper.xml
            1. <?xml version="1.0" encoding="UTF-8" ?>
            2. <!DOCTYPE mapper
            3.   PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
            4. <mapper namespace="com.njupt.mapper.StudentMapper"> <!-- 接口全類名 -->
            5.      <select id="getStudentById" resultType="com.njupt.entities.Student">
            6.            select * from student where id=#{id}
            7.      </select>
            8. </mapper>
      4. 讓SpringBoot查找到這兩個xml文件,需要在application.yml中配置以下兩項
        1. mybatis:
        2.   config-location: classpath:mybatis/config/mybatis-config.xml
        3.   mapper-locations:
        4.   - classpath:mybatis/mapper/*.xml
      5. 創建操作不同表對應的Mapper接口
        1. public interface StudentMapper {
        2.     public Student getStudentById(Integer id);
        3. }
      6. 批量掃描包下的mapper類
        1. @SpringBootApplication
        2. @MapperScan(value = "com.njupt.mapper")
        3. public class MybatisApplication {
        4.      public static void main(String[] args) {
        5.            SpringApplication.run(MybatisApplication.class, args);
        6.      }
        7. }
      7. 編寫業務代碼
        1. @Controller
        2. public class StudentController {
        3.    
        4.     @Autowired
        5.     private StudentMapper studentMapper;
        6.     private Logger logger = LoggerFactory.getLogger(this.getClass());
        7.    
        8.     @RequestMapping("/getStudentById/{id}")
        9.     @ResponseBody
        10.     public Student getStudentById(@PathVariable("id") Integer id) {
        11.         logger.info("enter getStudentById");
        12.         return studentMapper.getStudentById(id);
        13.     }
        14. }
      8. 測試
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章