快速创建Spring Boot应用
暂时先添加这么多依赖
项目生成之后打开pom文件看看
Spring Boot导入不进来,百度了一下,说要把*.lastUpdated文件删除。但我并没那么做,先去我的仓库看一看
发现Spring Boot的版本是2.1.8.RELEASE,估计是我之前已经安装过,所以我在新项目的文件把Spring Boot的版本改为2.1.8.RELEASE就ok了,如下图
好吧,先做个简单的测试
运行没成功
先把和数据库相关的包注释掉,然后重新启动程序
再次在浏览器中访问访问http://127.0.0.1:8080/hi
好了,一个简单的Spring Boot程序就搞定了。
下面我们继续整合MyBatis
首先,添加配置文件
添加完之后driver-class-name报红,如下图
添加mysql connector依赖解决报红问题
ok,问题解决
建立如下目录和文件
package com.mini.hi.bean;
import lombok.Data;
@Data
public class Student {
private Integer id;
private String gender;
private Integer class_id;
private String sname;
}
package com.mini.hi.controller;
import com.mini.hi.bean.Student;
import com.mini.hi.mapper.StudentMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RestController;
@RestController
public class StudentController {
@Autowired
StudentMapper studentMapper;
@GetMapping("/getStudentById/{id}")
public Student getStudentById(@PathVariable Integer id) {
return studentMapper.getStudentById(id);
}
@GetMapping("/getStudents")
public Student getStudents() {
return studentMapper.getStudents();
}
}
package com.mini.hi.mapper;
import com.mini.hi.bean.Student;
public interface StudentMapper {
Student getStudentById(Integer id);
Student getStudents();
}
在HiApplication添加MapperScan注解
<?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.mini.hi.mapper.StudentMapper">
<select id="getStudentById" resultType="com.mini.hi.bean.Student">
SELECT * FROM student WHERE id=#{id}
</select>
<select id="getStudents" resultType="com.mini.hi.bean.Student">
SELECT * FROM student
</select>
</mapper>
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE configuration PUBLIC "-//mybatis.org//DTD Config 3.0//EN" "http://mybatis.org/dtd/mybatis-3-config.dtd">
<configuration>
</configuration>
spring:
datasource:
username: root
password: 123456
url: jdbc:mysql://localhost/hi?useSSL=false&useUnicode=true&characterEncoding=UTF-8
driver-class-name: com.mysql.jdbc.Driver
mybatis:
config-location: classpath:mybatis/mybatis-config.xml
mapper-locations: classpath:mybatis/mapper/*.xml
好了,启动项目
访问http://127.0.0.1:8080/getStudents
接下来整合Druid
添加依赖
添加type: com.alibaba.druid.pool.DruidDataSource
新建AppDruidConfig类
package com.mini.hi.config;
import java.sql.SQLException;
import java.util.Arrays;
import java.util.HashMap;
import java.util.Map;
import javax.sql.DataSource;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.boot.web.servlet.FilterRegistrationBean;
import org.springframework.boot.web.servlet.ServletRegistrationBean;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import com.alibaba.druid.pool.DruidDataSource;
import com.alibaba.druid.support.http.StatViewServlet;
import com.alibaba.druid.support.http.WebStatFilter;
@Configuration
public class AppDruidConfig {
@ConfigurationProperties(prefix = "spring.datasource")
@Bean
public DataSource dataSource() throws SQLException {
DruidDataSource dataSource = new DruidDataSource();
dataSource.setFilters("stat"); //设置监控SQL的过滤器。
return dataSource;
}
@Bean
public ServletRegistrationBean statViewServlet() {
ServletRegistrationBean bean = new ServletRegistrationBean(new StatViewServlet(), "/druid/*");
Map<String, String> initParams = new HashMap<>();
initParams.put("loginUsername", "root");
initParams.put("loginPassword", "Aaron123456#");
initParams.put("allow", "");// 默认就是允许所有访问
bean.setInitParameters(initParams);
return bean;
}
@Bean
public FilterRegistrationBean webStatFilter() {
FilterRegistrationBean bean = new FilterRegistrationBean();
bean.setFilter(new WebStatFilter());
Map<String, String> initParams = new HashMap<>();
initParams.put("exclusions", "*.js,*.css,/druid/*");
bean.setInitParameters(initParams);
bean.setUrlPatterns(Arrays.asList("/*"));
return bean;
}
}
配置Druid完成
访问http://127.0.0.1:8080/druid/
至此,Spring Boot整合MyBatis和Druid成功!