Spring boot 快速入门篇(下篇–SpringMVC+Mybatis+Druid+Mysql)
前言
Springboot集成了springJDBC与JPA,但是没有集成mybatis,所以想要使用mybatis就要自己去集成。
主要是在Spring Boot中集成MyBatis,可以选用基于注解的方式,也可以选择xml文件配置的方式。官方推荐使用xml文件配置。
阅读本文需要了解一些Springboot和Spring、Mybatis的基础知识并会创建Springboot项目
项目使用Spring Boot 2.0.0 JDK 1.8
最终项目结构:
第一步:创建Spring boot项目(略)
第二步:在pom文件引入依赖:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 |
<dependency> <groupId>org.mybatis.spring.boot</groupId> <artifactId>mybatis-spring-boot-starter</artifactId> <version>1.2.0</version> </dependency> <!-- https://mvnrepository.com/artifact/mysql/mysql-connector-java --> <dependency> <groupId>mysql</groupId> <artifactId>mysql-connector-java</artifactId> <version>5.1.29</version> </dependency> <!-- https://mvnrepository.com/artifact/com.alibaba/druid --> <dependency> <groupId>com.alibaba</groupId> <artifactId>druid</artifactId> <version>1.0.29</version> </dependency> |
第三步:application.properties添加配置:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 |
server.port=8081 server.servlet.context-path=/test server.tomcat.uri-encoding=UTF-8 spring.http.encoding.charset=UTF-8 spring.http.encoding.enabled=true spring.http.encoding.force=true spring.messages.encoding=UTF-8 spring.datasource.type=com.alibaba.druid.pool.DruidDataSource spring.datasource.driverClassName=com.mysql.jdbc.Driver spring.datasource.url=jdbc:mysql://localhost:3306/testdb spring.datasource.username=root spring.datasource.password=123456 mybatis.mapper-locations=classpath:mapper/*.xml |
其中
1 2 |
server.port=8081 server.servlet.context-path=/test |
这两项可以不配置
第四步:创建dao:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 |
package net.code2048.dao; import org.apache.ibatis.annotations.Mapper; import org.apache.ibatis.annotations.Select; @Mapper /** * Created by poxiao on 2018/3/19. */ public interface ITestDao { @Select("select sysdate()") public String getDate(); public String getSysDate(); } |
第五步:创建Mapper文件(TestMapper.xml):
1 2 3 4 5 6 7 8 9 10 |
<?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="net.code2048.dao.ITestDao" > <select id="getSysDate" resultType="String" > select sysdate() </select> </mapper> |
第六步:创建service
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 |
package net.code2048.service; import net.code2048.dao.ITestDao; import org.springframework.stereotype.Service; import javax.annotation.Resource; @Service /** * Created by poxiao on 2018/3/19. */ public class TestService { @Resource private ITestDao testDao; public String getString(String key) { return testDao.getDate() + "【" + key + "】" + testDao.getSysDate(); } } |
第七步:创建controller
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 |
package net.code2048.controller; import net.code2048.service.TestService; import org.springframework.stereotype.Controller; import org.springframework.web.bind.annotation.*; import javax.annotation.Resource; import javax.servlet.http.HttpServletResponse; /** * Created by poxiao on 2018/3/16. */ @RequestMapping("c1") @Controller public class TestController { @Resource private TestService testService; @RequestMapping(value = "/a1.php", method = RequestMethod.GET) public @ResponseBody String getInfo(@RequestParam String key, HttpServletResponse response) throws Exception { return testService.getString(key); } } |
当然此处Controller注解可以使用RestController注解 然后去掉ResponseBody注解 此处使用Controller是为了把之前项目移植到Spring Boot里
第八步:为了让加入的controller,service,Mapper等能够被扫描到,需要在Application类中加入注解:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 |
package net.code2048.run; import org.mybatis.spring.annotation.MapperScan; import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; import org.springframework.context.annotation.ComponentScan; @ComponentScan(value = {"net.code2048.controller", "net.code2048.service"}) @MapperScan(basePackages = "net.code2048.dao") @SpringBootApplication public class DemoApplication { public static void main(String[] args) { SpringApplication.run(DemoApplication.class, args); } } |
项目配置完毕,运行一下试试:
打开测试链接:http://127.0.0.1:8081/test/c1/a1.php?key=aaa
展示数据库系统时间
补充:如果需要查看druid监控页面可以加上servlet
首先创建DruidStatViewServlet
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 |
package net.code2048.run; import com.alibaba.druid.support.http.StatViewServlet; import javax.servlet.annotation.WebInitParam; import javax.servlet.annotation.WebServlet; /** * * @Description: 监控数据库性能 */ @SuppressWarnings("serial") @WebServlet(urlPatterns = "/druid/*", initParams={ @WebInitParam(name="allow",value="192.168.16.110,127.0.0.1"),// IP白名单 (没有配置或者为空,则允许所有访问) @WebInitParam(name="deny",value="192.168.16.111"),// IP黑名单 (存在共同时,deny优先于allow) @WebInitParam(name="loginUsername",value="test"),// 用户名 @WebInitParam(name="loginPassword",value="test"),// 密码 @WebInitParam(name="resetEnable",value="false")// 禁用HTML页面上的“Reset All”功能 }) public class DruidStatViewServlet extends StatViewServlet { } |
然后创建一个过滤器DruidStatFilter :
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 |
package net.code2048.run; import com.alibaba.druid.support.http.WebStatFilter; import javax.servlet.annotation.WebFilter; import javax.servlet.annotation.WebInitParam; /** * * @Description: 拦截druid监控请求 */ @WebFilter(filterName="druidWebStatFilter",urlPatterns="/*", initParams={ @WebInitParam(name="exclusions",value="*.js,*.gif,*.jpg,*.bmp,*.png,*.css,*.ico,/druid/*")// 忽略资源 }) public class DruidStatFilter extends WebStatFilter { } |
然后加入Servlet自动扫描 ServletComponentScan
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 |
package net.code2048.run; import org.mybatis.spring.annotation.MapperScan; import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; import org.springframework.boot.web.servlet.ServletComponentScan; import org.springframework.context.annotation.ComponentScan; @ComponentScan(value = {"net.code2048.controller", "net.code2048.service"}) @MapperScan(basePackages = "net.code2048.dao") @ServletComponentScan @SpringBootApplication public class DemoApplication { public static void main(String[] args) { SpringApplication.run(DemoApplication.class, args); } } |
OK 运行一下,打开http://127.0.0.1:8081/test/druid/login.html
输入配置的用户名密码:test
1 2 |
@WebInitParam(name="loginUsername",value="test"),// 用户名 @WebInitParam(name="loginPassword",value="test"),// 密码 |
上面数据连接池配置都是最简单配置,下面属性可按需配置:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 |
#连接池的配置信息 spring.datasource.initialSize=5 spring.datasource.minIdle=5 spring.datasource.maxActive=20 spring.datasource.maxWait=60000 spring.datasource.timeBetweenEvictionRunsMillis=60000 spring.datasource.minEvictableIdleTimeMillis=300000 spring.datasource.validationQuery=SELECT 1 FROM DUAL spring.datasource.testWhileIdle=true spring.datasource.testOnBorrow=false spring.datasource.testOnReturn=false spring.datasource.poolPreparedStatements=true spring.datasource.maxPoolPreparedStatementPerConnectionSize=20 spring.datasource.filters=stat,wall,log4j spring.datasource.connectionProperties=druid.stat.mergeSql=true;druid.stat.slowSqlMillis=5000 |
本文链接:Spring boot 快速入门篇(下篇--SpringMVC+Mybatis+Druid+Mysql)
转载声明:本站文章若无特别说明,皆为原创,转载请注明来源:破晓(http://www.code2048.net),谢谢!^^