springboot整合mybatis+jetty笔记以及遇到的问题
-
pom.xml
-
1 <dependencies> 2 <!--<dependency>--> 3 <!--<groupId>org.springframework.boot</groupId>--> 4 <!--<artifactId>spring-boot-starter-data-jpa</artifactId>--> 5 <!--</dependency>--> 6 <dependency> 7 <groupId>org.springframework.boot</groupId> 8 <artifactId>spring-boot-starter-web</artifactId> 9 <exclusions> 10 <exclusion> 11 <groupId>org.springframework.boot</groupId> 12 <artifactId>spring-boot-starter-tomcat</artifactId> 13 </exclusion> 14 </exclusions> 15 </dependency> 16 17 <dependency> 18 <groupId>org.springframework.boot</groupId> 19 <artifactId>spring-boot-starter-jetty</artifactId> 20 </dependency> 21 22 <dependency> 23 <groupId>org.mybatis.spring.boot</groupId> 24 <artifactId>mybatis-spring-boot-starter</artifactId> 25 <version>1.3.2</version> 26 </dependency> 27 28 <dependency> 29 <groupId>mysql</groupId> 30 <artifactId>mysql-connector-java</artifactId> 31 <!--<scope>runtime</scope>--> 32 </dependency> 33 34 <dependency> 35 <groupId>org.projectlombok</groupId> 36 <artifactId>lombok</artifactId> 37 <optional>true</optional> 38 </dependency> 39 40 <dependency> 41 <groupId>org.springframework.boot</groupId> 42 <artifactId>spring-boot-starter-test</artifactId> 43 <scope>test</scope> 44 </dependency> 45 46 <dependency> 47 <groupId>org.springframework.boot</groupId> 48 <artifactId>spring-boot-autoconfigure</artifactId> 49 <version>2.1.0.RELEASE</version> 50 </dependency> 51 52 <dependency> 53 <groupId>javax.servlet</groupId> 54 <artifactId>javax.servlet-api</artifactId> 55 <version>3.1.0</version> 56 </dependency> 57 58 <dependency> 59 <groupId>com.google.guava</groupId> 60 <artifactId>guava</artifactId> 61 <version>19.0</version> 62 <!--<version>1.1.9</version>--> 63 </dependency> 64 65 <!--阿里巴巴用来监控mysql线程池--> 66 <dependency> 67 <groupId>com.alibaba</groupId> 68 <artifactId>druid</artifactId> 69 <version>1.1.9</version> 70 </dependency> 71 72 </dependencies> 73 74 <build> 75 <plugins> 76 <plugin> 77 <groupId>org.springframework.boot</groupId> 78 <artifactId>spring-boot-maven-plugin</artifactId> 79 </plugin> 80 </plugins> 81 </build>
-
-
application.properties配置数据库
-
1 server.port=8090 2 ## 数据源配置 3 spring.datasource.url=jdbc:mysql://localhost:3306/xxx?characterEncoding=utf-8&useSSL=false&serverTimezone=GMT%2b8 4 spring.datasource.username=root 5 spring.datasource.password=123456 6 #spring.datasource.driver-class-name=com.mysql.jdbc.Driver 7 spring.datasource.driver-class-name=com.mysql.cj.jdbc.Driver 8 mybatis.config-location=classpath:/mybatis/mybatis-config.xml
-
有时候url只是配置characterEncoding=utf-8参数 会报错
-
-
mybaits.xml的配置
-
1 <?xml version="1.0" encoding="UTF-8"?> 2 <!DOCTYPE configuration PUBLIC "-//mybatis.org//DTD Config 3.0//EN" 3 "http://mybatis.org/dtd/mybatis-3-config.dtd"> 4 <configuration> 5 <settings> 6 <!--配置关闭缓存--> 7 <!--缓存开关--> 8 <setting name="cacheEnabled" value="false"/> 9 <!--自动启动驼峰原则--> 10 <setting name="mapUnderscoreToCamelCase" value="true"/> 11 <!-- 允许 JDBC 支持自动生成主键,需要驱动兼容。 如果设置为 true 则这个设置强制使用自动生成主键,尽管一些驱动不能兼容但仍可正常工作(比如 Derby)。 默认值false --> 12 <setting name="useGeneratedKeys" value="true"/> 13 <!-- 配置默认的执行器。SIMPLE 就是普通的执行器;REUSE 执行器会重用预处理语句(prepared statements); BATCH 执行器将重用语句并执行批量更新。默认SIMPLE --> 14 <setting name="defaultExecutorType" value="REUSE"/> 15 <!-- 设置超时时间,它决定驱动等待数据库响应的秒数。 --> 16 <setting name="defaultStatementTimeout" value="600"/> 17 18 </settings> 19 20 <typeAliases > 21 <typeAlias type="com.mooc.house.common.model.User" alias="user"></typeAlias> 22 </typeAliases> 23 24 <mappers> 25 <mapper resource="mapper/myUser.xml"/> 26 </mappers> 27 28 </configuration>
-
-
mapper xml的配置
-
1 <?xml version="1.0" encoding="UTF-8" ?> 2 <!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd" > 3 <mapper 4 namespace="com.xx.xx.mapper.UserMapper"> 5 <select id="xxx" resultType="user"> 6 select xxx from user 7 </select> 8 </mapper>
-
-
controller层
-
1 @RestController 2 public class UserController { 3 @Autowired 4 private Userservice userservice; 5 6 @RequestMapping("get") 7 public List<User> getUsers(){ 8 return userservice.getUsers(); 9 } 10 11 }
-
-
service层
-
1 @Service 2 public class Userservice { 3 @Resource 4 private UserMapper userMapper; 5 6 public List<User> getUsers(){ 7 return userMapper.findAllUsers(); 8 } 9 }
-
-
mapper 接口层
-
1 @Mapper 2 public interface UserMapper { 3 // 查询用户 4 public List<User> findAllUsers(); 5 }
-
-
启动jetty
-
返回json
作者:以罗伊
本文版权归作者和博客园共有,欢迎转载,但未经作者同意必须在文章页面给出原文链接,否则保留追究法律责任的权利。