标签:pac ORC 关于 scope www 没有 neu inno als
前言:
本文是在自己整合springboot 2.0 和mybatis时的过程和踩得坑。
先附上github地址:https://github.com/yclxt/springboot-mybatis.git
环境/版本:
工具: Intellij IDEA 2018.3
JDK: 1.8
Springboot: 2.0.4.RELEASE
Mybatis : 1.3.2
由于本人是初学者,对druid 和handle Page 不太熟悉,所以只是按照网上的配置加了上去,等了解后会在之后的博客中提出。
创建及整合:
Step1:用IDEA创建一个Spring Initializr 项目,按如图所示配置。
pom.xml:
1 <?xml version="1.0" encoding="UTF-8"?> 2 <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 3 xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> 4 <modelVersion>4.0.0</modelVersion> 5 6 <groupId>com.example</groupId> 7 <artifactId>demo</artifactId> 8 <version>0.0.1-SNAPSHOT</version> 9 <packaging>jar</packaging> 10 11 <name>demo</name> 12 <description>Demo project for Spring Boot</description> 13 14 <parent> 15 <groupId>org.springframework.boot</groupId> 16 <artifactId>spring-boot-starter-parent</artifactId> 17 <version>2.0.4.RELEASE</version> 18 <relativePath/> <!-- lookup parent from repository --> 19 </parent> 20 21 <properties> 22 <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding> 23 <project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding> 24 <java.version>1.8</java.version> 25 </properties> 26 27 <dependencies> 28 <dependency> 29 <groupId>org.springframework.boot</groupId> 30 <artifactId>spring-boot-starter-web</artifactId> 31 </dependency> 32 <dependency> 33 <groupId>org.mybatis.spring.boot</groupId> 34 <artifactId>mybatis-spring-boot-starter</artifactId> 35 <version>1.3.2</version> 36 </dependency> 37 <dependency> 38 <groupId>mysql</groupId> 39 <artifactId>mysql-connector-java</artifactId> 40 <scope>runtime</scope> 41 </dependency> 42 <dependency> 43 <groupId>org.springframework.boot</groupId> 44 <artifactId>spring-boot-starter-test</artifactId> 45 <scope>test</scope> 46 </dependency> 47 <dependency> 48 <groupId>org.apache.commons</groupId> 49 <artifactId>commons-lang3</artifactId> 50 <version>3.4</version> 51 </dependency> 52 53 54 <dependency> 55 <groupId>com.fasterxml.jackson.core</groupId> 56 <artifactId>jackson-core</artifactId> 57 </dependency> 58 <dependency> 59 <groupId>com.fasterxml.jackson.core</groupId> 60 <artifactId>jackson-databind</artifactId> 61 </dependency> 62 <dependency> 63 <groupId>com.fasterxml.jackson.datatype</groupId> 64 <artifactId>jackson-datatype-joda</artifactId> 65 </dependency> 66 <dependency> 67 <groupId>com.fasterxml.jackson.module</groupId> 68 <artifactId>jackson-module-parameter-names</artifactId> 69 </dependency> 70 <!-- 分页插件 --> 71 <dependency> 72 <groupId>com.github.pagehelper</groupId> 73 <artifactId>pagehelper-spring-boot-starter</artifactId> 74 <version>1.2.5</version> 75 </dependency> 76 <!-- alibaba的druid数据库连接池 --> 77 <dependency> 78 <groupId>com.alibaba</groupId> 79 <artifactId>druid-spring-boot-starter</artifactId> 80 <version>1.1.9</version> 81 </dependency> 82 </dependencies> 83 84 <build> 85 <plugins> 86 <plugin> 87 <groupId>org.springframework.boot</groupId> 88 <artifactId>spring-boot-maven-plugin</artifactId> 89 </plugin> 90 91 <!-- mybatis generator 自动生成代码插件 --> 92 <plugin> 93 <groupId>org.mybatis.generator</groupId> 94 <artifactId>mybatis-generator-maven-plugin</artifactId> 95 <version>1.3.2</version> 96 <configuration> 97 <configurationFile>${basedir}/src/main/resources/generator/generatorConfig.xml</configurationFile> 98 <overwrite>true</overwrite> 99 <verbose>true</verbose> 100 </configuration> 101 </plugin> 102 </plugins> 103 </build> 104 105 106 </project>
可以根据需要自行添加删除。
Step2:创建数据库表
1 CREATE TABLE `user` ( 2 `Id` int(11) NOT NULL AUTO_INCREMENT, 3 `username` varchar(50) DEFAULT NULL, 4 `gender` varchar(4) DEFAULT NULL, 5 `age` int(3) DEFAULT NULL, 6 PRIMARY KEY (`Id`) 7 ) ENGINE=InnoDB AUTO_INCREMENT=1003 DEFAULT CHARSET=utf8;
Step3:使用generator-mybatis工具反向生产实体类,dao及相关xml文件:
在resource中新建xml文件:generatorConfig.xml,记得修改信息以满足自己的项目。
<?xml version="1.0" encoding="UTF-8"?> <!DOCTYPE generatorConfiguration PUBLIC "-//mybatis.org//DTD MyBatis Generator Configuration 1.0//EN" "http://mybatis.org/dtd/mybatis-generator-config_1_0.dtd"> <generatorConfiguration> <!-- 数据库驱动:选择你的本地硬盘上面的数据库驱动包--> <classPathEntry location="E:\maven\repository\mysql\mysql-connector-java\5.1.41\mysql-connector-java-5.1.41.jar"/> <context id="DB2Tables" targetRuntime="MyBatis3"> <commentGenerator> <property name="suppressDate" value="true"/> <!-- 是否去除自动生成的注释 true:是 : false:否 --> <property name="suppressAllComments" value="true"/> </commentGenerator> <!--数据库链接URL,用户名、密码 --> <jdbcConnection driverClass="com.mysql.jdbc.Driver" connectionURL="jdbc:mysql://127.0.0.1:3306/mydb" userId="root" password="123"> </jdbcConnection> <javaTypeResolver> <property name="forceBigDecimals" value="false"/> </javaTypeResolver> <!-- 生成模型的包名和位置--> <javaModelGenerator targetPackage="com.example.demo.entity" targetProject="src/main/java"> <property name="enableSubPackages" value="true"/> <property name="trimStrings" value="true"/> </javaModelGenerator> <!-- 生成映射文件的包名和位置--> <sqlMapGenerator targetPackage="mapping" targetProject="src/main/resources"> <property name="enableSubPackages" value="true"/> </sqlMapGenerator> <!-- 生成DAO的包名和位置--> <javaClientGenerator type="XMLMAPPER" targetPackage="com.example.demo.entityMapper" targetProject="src/main/java"> <property name="enableSubPackages" value="true"/> </javaClientGenerator> <!-- 要生成的表 tableName是数据库中的表名或视图名 domainObjectName是实体类名--> <table tableName="user" domainObjectName="User" enableCountByExample="false" enableUpdateByExample="false" enableDeleteByExample="false" enableSelectByExample="false" selectByExampleQueryId="false"> </table> </context> </generatorConfiguration>
点击工具栏上的Run——EditConfigrations,选择maven(没有则点击左上角+号),填写如图信息并OK.
选择运行名称,点击运行。
这样就可以得到 实体类,dao 及对应的xml文件
Step4:添加service、controller
UserService.java
package com.example.demo.Service; import com.example.demo.entity.User; import java.util.List; public interface UserService { int addUser(User user); User findUserById(Integer Id); List<User> getAllUsers();//UserMapper.xml中记得添加这个方法 int updateUser(User user); int deleteUserById(Integer Id); }
UserServiceImpl.java
package com.example.demo.Service.impl; import com.example.demo.Service.UserService; import com.example.demo.entity.User; import com.example.demo.entityMapper.UserMapper; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Repository; import org.springframework.stereotype.Service; import java.util.List; @Service(value = "userService") public class UserServiceImpl implements UserService { @Autowired private UserMapper userMapper; @Override public int addUser(User user) { return userMapper.insert(user); } @Override public User findUserById(Integer Id) { return userMapper.selectByPrimaryKey(Id); } @Override public List<User> getAllUsers() { return userMapper.selectAllUsers(); } @Override public int updateUser(User user) { return userMapper.updateByPrimaryKey(user); } @Override public int deleteUserById(Integer Id) { return userMapper.deleteByPrimaryKey(Id); } }
UserController.java
package com.example.demo.controller; import com.example.demo.Service.UserService; import com.example.demo.entity.User; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Controller; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.ResponseBody; import org.springframework.web.bind.annotation.RestController; import java.util.List; @Controller @RequestMapping(value = "user") public class UserController { @Autowired private UserService userService; @ResponseBody @RequestMapping(value = "/add") public int addUser(User user){ return userService.addUser(user); } @ResponseBody @RequestMapping(value = "/all", produces = {"application/json;charset=UTF-8"}) public List<User> getAllUsers(){ return userService.getAllUsers(); } @ResponseBody @RequestMapping(value = "/update") public int updateUser(User user){ return userService.updateUser(user); } @ResponseBody @RequestMapping(value = "/") public User getOneUser(Integer Id){ return userService.findUserById(Id); } @ResponseBody @RequestMapping(value = "/delete") public int deleteUser(Integer Id){ return userService.deleteUserById(Id); } }
Step5:配置application.properties/application.yml
本文使用yml; properties是将参数写成K = V的形式,而yml则是树状结构,更加清晰。
application.yml:
1 server: 2 port: 8080 3 4 5 spring: 6 datasource: 7 name: mysql_test 8 type: com.alibaba.druid.pool.DruidDataSource 9 #druid相关配置 10 druid: 11 #监控统计拦截的filters 12 filters: stat 13 driver-class-name: com.mysql.jdbc.Driver 14 #基本属性 15 url: jdbc:mysql://127.0.0.1:3306/mydb?useUnicode=true&characterEncoding=UTF-8&allowMultiQueries=true 16 username: root 17 password: 123 18 #配置初始化大小/最小/最大 19 initial-size: 1 20 min-idle: 1 21 max-active: 20 22 #获取连接等待超时时间 23 max-wait: 60000 24 #间隔多久进行一次检测,检测需要关闭的空闲连接 25 time-between-eviction-runs-millis: 60000 26 #一个连接在池中最小生存的时间 27 min-evictable-idle-time-millis: 300000 28 validation-query: SELECT ‘x‘ 29 test-while-idle: true 30 test-on-borrow: false 31 test-on-return: false 32 #打开PSCache,并指定每个连接上PSCache的大小。oracle设为true,mysql设为false。分库分表较多推荐设置为false 33 pool-prepared-statements: false 34 max-pool-prepared-statement-per-connection-size: 20 35 36 mybatis: 37 mapper-locations: classpath:mapping/*.xml 38 type-aliases-package: com.example.demo.entity 39 40 #pagehelper 41 pagehelper: 42 helperDialect: mysql 43 reasonable: true 44 supportMethodsArguments: true 45 params: count=countSql 46 returnPageInfo: check
Step6:启动项目,测试CRUD功能。
这里只展示查询,其余请自行尝试。
最终的项目结构:
其他注意点:
关于实体类(User.java) 要加上@Mapper注解,并在启动类(DemoApplication.java)添加 @MapperScan(basePackages = "com.example.demo.entityMapper" ) 注解,此处不贴代码,读者可以查看git上的源码。
遇到的问题:
Q1:在generator反向生成时,generatorConfig.xml 中的http://mybatis.org/dtd/mybatis-generator-config_1_0.dtd 不能识别导致生成失败。
A1:在IDEA的File—setting中,找到Languages&Frameworks—Schemas and DTDs,右侧的External Schemas and DTDs窗口中点击添加按钮,然后url上把链接粘贴上 。
Q2:遇到org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name *******(省略)
A2:检查注解是否正确;
检查yml文件,是否将mybatis作为树状结构的父节点(顶头写);(楼主遇到的状况)
如果是properties文件遇到该问题,检查参数是否正确;
Q3: 遇到org.apache.ibatis.binding.BindingException: Invalid bound statement (not found)
A3:检查yml关于mybatis的配置,mapper-locations 后的路径是否和resource下存放xxMapper.xml的路径相同。
这个问题的相关解决方法在下面的参考来源Q3中有。
参考来源:Q1:https://blog.csdn.net/gaoqiang1112/article/details/78273776
Q3: https://blog.csdn.net/softwarehe/article/details/8889206
https://www.jianshu.com/p/5cd772c07041
=========================2018/08/16更新===================
添加热部署功能:https://www.cnblogs.com/sprinkle/p/7058630.html
这是本人第一次写博客,如有问题请提出,谢谢教正。
【学习】整合springboot2.0 和 mybatis,实现基本的CRUD
标签:pac ORC 关于 scope www 没有 neu inno als
原文地址:https://www.cnblogs.com/lxt97/p/9485978.html