1.配置pom.xml文件css
<?xml version="1.0" encoding="UTF-8"?> <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> <modelVersion>4.0.0</modelVersion> <parent> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-parent</artifactId> <version>2.1.4.RELEASE</version> <relativePath/> <!-- lookup parent from repository --> </parent> <groupId>com.czhappy.bootlearn</groupId> <artifactId>03-Spring-Boot-MyBatis</artifactId> <version>0.0.1-SNAPSHOT</version> <name>03-Spring-Boot-MyBatis</name> <description>Demo project for Spring Boot</description> <properties> <java.version>1.8</java.version> </properties> <dependencies> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-test</artifactId> <scope>test</scope> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> </dependency> <!-- 數據庫 --> <dependency> <groupId>mysql</groupId> <artifactId>mysql-connector-java</artifactId> </dependency> <dependency> <groupId>org.mybatis.spring.boot</groupId> <artifactId>mybatis-spring-boot-starter</artifactId> <version>1.3.2</version> </dependency> <!-- Druid Pool --> <dependency> <groupId>com.alibaba</groupId> <artifactId>druid-spring-boot-starter</artifactId> <version>1.1.16</version> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-jdbc</artifactId> </dependency> </dependencies> <build> <plugins> <plugin> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-maven-plugin</artifactId> </plugin> </plugins> </build> </project>
2.修改配置文件application.yml:java
server: port: 8080 servlet: context-path: /web spring: datasource: druid: # 數據庫訪問配置, 使用druid數據源 type: com.alibaba.druid.pool.DruidDataSource driver-class-name: com.mysql.cj.jdbc.Driver url: jdbc:mysql://localhost:3306/springbootlearn?useUnicode=true&characterEncoding=utf8&useSSL=false&serverTimezone=UTC username: root password: root # 鏈接池配置 initial-size: 5 min-idle: 5 max-active: 20 # 鏈接等待超時時間 max-wait: 30000 # 配置檢測能夠關閉的空閒鏈接間隔時間 time-between-eviction-runs-millis: 60000 # 配置鏈接在池中的最小生存時間 min-evictable-idle-time-millis: 300000 validation-query: select 'x' test-while-idle: true test-on-borrow: false test-on-return: false # 打開PSCache,而且指定每一個鏈接上PSCache的大小 pool-prepared-statements: true max-open-prepared-statements: 20 max-pool-prepared-statement-per-connection-size: 20 # 配置監控統計攔截的filters, 去掉後監控界面sql沒法統計, 'wall'用於防火牆 filters: stat,wall # Spring監控AOP切入點,如x.y.z.service.*,配置多個英文逗號分隔 aop-patterns: com.springboot.servie.* # WebStatFilter配置 web-stat-filter: enabled: true # 添加過濾規則 url-pattern: /* # 忽略過濾的格式 exclusions: '*.js,*.gif,*.jpg,*.png,*.css,*.ico,/druid/*' # StatViewServlet配置 stat-view-servlet: enabled: true # 訪問路徑爲/druid時,跳轉到StatViewServlet url-pattern: /druid/* # 是否可以重置數據 reset-enable: false # 須要帳號密碼才能訪問控制檯 login-username: druid login-password: druid123 # IP白名單 # allow: 127.0.0.1 # IP黑名單(共同存在時,deny優先於allow) # deny: 192.168.1.218 # 配置StatFilter filter: stat: log-slow-sql: true
上述配置不但配置了Druid做爲鏈接池,並且還開啓了Druid的監控功能
3.運行項目mysql
訪問http://localhost:8080/web/druid:web
輸入登陸用戶名和密碼,能夠查看Druid的監控信息:spring
4.實現增刪改查功能:sql
建立測試表tb_student,並插入幾條測試數據數據庫
CREATE TABLE `tb_student` ( `id` int(10) unsigned NOT NULL AUTO_INCREMENT, `sno` varchar(10) DEFAULT NULL, `sname` varchar(100) DEFAULT NULL, `ssex` char(2) DEFAULT NULL, PRIMARY KEY (`id`) ) ENGINE=InnoDB AUTO_INCREMENT=4 DEFAULT CHARSET=utf8;
INSERT INTO `tb_student` VALUES ('1', '001', '張三', 'M'); INSERT INTO `tb_student` VALUES ('2', '002', '李四', 'M'); INSERT INTO `tb_student` VALUES ('3', '003', '王五', 'F');
建立對應實體:apache
package com.czhappy.bootlearn.SpringBootMyBatis.entity; import java.io.Serializable; public class Student implements Serializable { private static final long serialVersionUID = -339516038496531943L; private Integer id; private String sno; private String name; private String sex; public Integer getId() { return id; } public void setId(Integer id) { this.id = id; } public static long getSerialVersionUID() { return serialVersionUID; } public String getSno() { return sno; } public void setSno(String sno) { this.sno = sno; } public String getName() { return name; } public void setName(String name) { this.name = name; } public String getSex() { return sex; } public void setSex(String sex) { this.sex = sex; } }
使用註解方式建立StudentMapper:springboot
package com.czhappy.bootlearn.SpringBootMyBatis.mapper; import com.czhappy.bootlearn.SpringBootMyBatis.entity.Student; import org.apache.ibatis.annotations.*; import org.springframework.stereotype.Component; @Component @Mapper public interface StudentMapper { @Insert("insert into tb_student(sno,sname,ssex) values(#{sno},#{name},#{sex})") int add(Student student); @Update("update tb_student set sname=#{name},ssex=#{sex} where sno=#{sno}") int update(Student student); @Delete("delete from tb_student where sno=#{sno}") int deleteBysno(String sno); @Select("select * from tb_student where sno=#{sno}") @Results(id = "student",value= { @Result(property = "sno", column = "sno", javaType = String.class), @Result(property = "name", column = "sname", javaType = String.class), @Result(property = "sex", column = "ssex", javaType = String.class) }) Student queryStudentBySno(String sno); }
建立service:mybatis
package com.czhappy.bootlearn.SpringBootMyBatis.service; import com.czhappy.bootlearn.SpringBootMyBatis.entity.Student; public interface StudentService { int add(Student student); int update(Student student); int deleteBysno(String sno); Student queryStudentBySno(String sno); }
package com.czhappy.bootlearn.SpringBootMyBatis.service.impl; import com.czhappy.bootlearn.SpringBootMyBatis.entity.Student; import com.czhappy.bootlearn.SpringBootMyBatis.mapper.StudentMapper; import com.czhappy.bootlearn.SpringBootMyBatis.service.StudentService; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Service; @Servicepublic class StudentServiceImp implements StudentService { @Autowired private StudentMapper studentMapper; @Override public int add(Student student) { return this.studentMapper.add(student); } @Override public int update(Student student) { return this.studentMapper.update(student); } @Override public int deleteBysno(String sno) { return this.studentMapper.deleteBysno(sno); } @Override public Student queryStudentBySno(String sno) { return this.studentMapper.queryStudentBySno(sno); } }
建立測試controller:
package com.czhappy.bootlearn.SpringBootMyBatis.controller; import com.czhappy.bootlearn.SpringBootMyBatis.entity.Student; import com.czhappy.bootlearn.SpringBootMyBatis.service.StudentService; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RequestMethod; import org.springframework.web.bind.annotation.RestController; @RestController public class TestController { @Autowired private StudentService studentService; @RequestMapping( value = "/querystudent", method = RequestMethod.GET) public Student queryStudentBySno(String sno) { return this.studentService.queryStudentBySno(sno); } }
重啓項目,測試查詢接口: