<h4 style="">来自:<a href="https://blog.csdn.net/chajinglong/article/details/52795674" target="_blank">https://blog.csdn.net/chajinglong/article/details/52795674</a></h4><h4 style="">前言</h4><p style="">最近做回后台开发,重新抓起以前学过的SSM(Spring+Spring MVC+Mybatis),但是发现配置实在过于复杂,好多东西配置起来麻烦,虽然最终是配置出来了,但是还是感觉开发速度跟不上,本来打算切换到jfianl,但是后来发现需要用的几个框架不支持jfianl,如Swagger2(根据代码中的注解生成接口文档和测试页面,非常的方便);同时我也不愿意放弃SpringMVC强大的验证参数模块,jfianl中好像只能手动验证(当然我对jfianl只处于简单的开发,并不是特别熟),而SpringMVC中,直接就能通过注解来确定哪些参数是必须的,哪些不是必须的,这对于写接口的人来说,方便了很多,所以决定还是使用Spring家族的东西,下面先一一介绍下本文的主角们</p><h5 style=""><a target="_blank" href="http://projects.spring.io/spring-boot/" style="color:rgb(74,74,74); background:transparent">Spring boot</a></h5><p style="">spring-boot是由<a target="_blank" href="https://pivotal.io/" style="color:rgb(74,74,74); background:transparent">Pivotal</a>团队提供的全新框架,其设计目的是用来简化新Spring应用的初始搭建以及开发过程。该框架使用了特定的方式来进行配置,从而使开发人员不再需要定义样板化的配置。通过这种方式,Boot致力于在蓬勃发展的快速应用开发领域(rapid application development)成为领导者</p><p style="">多年以来,<a target="_blank" href="http://spring.io/" style="color:rgb(74,74,74); background:transparent">Spring IO</a>平台饱受非议的一点就是大量的XML配置以及复杂的依赖管理。在2013年的<a target="_blank" href="https://www.youtube.com/watch?v=jplkJIHPGos" style="color:rgb(74,74,74); background:transparent">SpringOne 2GX</a>会议上,Pivotal的CTO Adrian Colyer回应了这些批评,并且特别提到该平台将来的目标之一就是实现免XML配置的开发体验。Boot所实现的功能超出了这个任务的描述,开发人员不仅不再需要编写XML,而且在一些场景中甚至不需要编写繁琐的import语句。在对外公开的beta版本刚刚发布之时,Boot描述了如何使用该框架在140个字符内实现可运行的web应用,从而获得了极大的关注度,该样例发表在<a target="_blank" href="https://twitter.com/rob_winch/status/364871658483351552" style="color:rgb(74,74,74); background:transparent">tweet</a>上</p><p style="">当然上面又是我抄过来的,因为我觉得要我去介绍一个框架是什么的时候,我只会说我的理解,但是我的理解不能让大家知道它的背景,所以抄了上面那段,那么下面就是我理解的Spring boot是个什么东西</p><p style=""><span style="font-weight:700">无需繁琐配置的Spring集合包</span>&nbsp;Springboot本身并不是什么新的框架,它可以说只是Spring大家族的一个集合包,当然这个集合包吧基础的配置都给我配置好了,我们无需再进行繁琐的xml配置,甚至是都不用配置web.xml,因为springboot内部自己集成了一个tomcat,直接通过run的方式就能启动,打包也一样,都可以不用部署tomcat了,当然是针对小项目,大项目肯定是要对中间件进行一些优化的</p><h5 style=""><a target="_blank" href="https://github.com/mybatis/mybatis-3" style="color:rgb(74,74,74); background:transparent">Mybatis</a></h5><p style="">MyBatis是一个支持普通SQL查询,存储过程和高级映射的优秀持久层框架。MyBatis消除了几乎所有的JDBC代码和参数的手工设置以及对结果集的检索封装。MyBatis可以使用简单的XML或注解用于配置和原始映射,将接口和Java的POJO(Plain Old Java Objects,普通的Java对象)映射成数据库中的记录。当然我个人更倾向用注解,因为实在是不怎么喜欢配置xml,尤其是eclipse经常因为xml的一些问题卡主,导致编译要等xml验证通过后才能编译,而我用Mybatis最主要的地方就是因为不用像JDBC那样自己一个属性一个属性来赋值</p><h5 style=""><a target="_blank" href="https://github.com/alibaba/druid" style="color:rgb(74,74,74); background:transparent">Druid</a></h5><p style="">Druid是一个JDBC组件,它包括三部分:</p><p style="">DruidDriver 代理Driver,能够提供基于Filter-Chain模式的插件体系。&nbsp;<br/>DruidDataSource 高效可管理的数据库连接池。&nbsp;<br/>SQLParser&nbsp;<br/>Druid可以做什么?</p><p style="">1) 可以监控数据库访问性能,Druid内置提供了一个功能强大的StatFilter插件,能够详细统计SQL的执行性能,这对于线上分析数据库访问性能有帮助。</p><p style="">2) 替换DBCP和C3P0。Druid提供了一个高效、功能强大、可扩展性好的数据库连接池。</p><p style="">3) 数据库密码加密。直接把数据库密码写在配置文件中,这是不好的行为,容易导致安全问题。DruidDruiver和DruidDataSource都支持PasswordCallback。</p><p style="">4) SQL执行日志,Druid提供了不同的LogFilter,能够支持Common-Logging、Log4j和JdkLog,你可以按需要选择相应的LogFilter,监控你应用的数据库访问情况。</p><p style="">扩展JDBC,如果你要对JDBC层有编程的需求,可以通过Druid提供的Filter-Chain机制,很方便编写JDBC层的扩展插件。</p><p style="">其实说简单点Druid就是一个<span style="font-weight:700">功能强大,性能优秀的数据库连接池</span>,是由阿里巴巴的大牛们开发的,除了性能好之外,我最喜欢的就是它的监控功能了,连github上的说明都是“为监控而生的数据库连接池!”</p><h5 style=""><a target="_blank" href="http://swagger.io/" style="color:rgb(74,74,74); background:transparent">Swagger2</a></h5><p style="">关于<a target="_blank" href="http://www.raye.wang/2016/09/29/swaggerhuan-jing-da-jian-zhi-fei-yi-lai-dai-ma-fa/" style="color:rgb(74,74,74); background:transparent">Swagger的介绍和非代码搭建</a>,在之前的文章已经说过了,不过这里的Swagger2只是版本号为2而已,很多核心的东西都没怎么变化,当然如果不用和代码封装在一起,也可以参考<a target="_blank" href="http://www.raye.wang/2016/09/29/swaggerhuan-jing-da-jian-zhi-fei-yi-lai-dai-ma-fa/" style="color:rgb(74,74,74); background:transparent">Swagger的介绍和非代码搭建</a>,不过本文主要讲述在代码中集成,所以不再过多描述</p><h5 style=""><a target="_blank" href="http://www.mybatis.org/generator/" style="color:rgb(74,74,74); background:transparent">mybatis-generator</a></h5><p style="">mybatis-generator是用来根据数据自动生成实体bean和一些常规查询语句的插件,有了这个就不用再一个一个实体bean来写了,而且普通的查询也可以使用自动生成sql语句查询出来</p><h4 style="">开始搭建</h4><h5 style="">一、创建项目</h5><p style="">首先创建一个maven项目,当然最好创建maven-archetype-webapp,原因无他,主要是因为很多时候我还是需要把项目部署到优化过配置的tomcat获取其他容器中,当然也可以创建普通的maven项目</p><h5 style="">二、添加依赖</h5><p>XML</p><pre class=" language-xml" style="font-family:Consolas,Monaco,&#39;Andale Mono&#39;,&#39;Ubuntu Mono&#39;,monospace; font-size:0.9em; margin-top:0.5em; margin-bottom:0.5em; border:1px solid rgb(227,237,243); width:1178px; padding:1em; overflow:auto; color:rgb(248,248,242); word-spacing:normal; word-break:normal; word-wrap:normal; line-height:1.5; background:rgb(39,40,34)">&lt;!–&nbsp;Springboot&nbsp;–&gt; &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&lt;dependency&gt; &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&lt;groupId&gt;org.springframework.boot&lt;/groupId&gt; &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&lt;artifactId&gt;spring-boot-starter-web&lt;/artifactId&gt; &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&lt;/dependency&gt; &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&lt;dependency&gt; &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&lt;groupId&gt;org.springframework.boot&lt;/groupId&gt; &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&lt;artifactId&gt;spring-boot-starter-jdbc&lt;/artifactId&gt; &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&lt;/dependency&gt; &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&lt;dependency&gt; &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&lt;groupId&gt;org.springframework.boot&lt;/groupId&gt; &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&lt;artifactId&gt;spring-boot-starter-aop&lt;/artifactId&gt; &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&lt;/dependency&gt; &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&lt;!–&nbsp;Springboot&nbsp;热部署&nbsp;–&gt; &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&lt;dependency&gt; &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&lt;groupId&gt;org.springframework.boot&lt;/groupId&gt; &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&lt;artifactId&gt;spring-boot-devtools&lt;/artifactId&gt; &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&lt;optional&gt;true&lt;/optional&gt; &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&lt;/dependency&gt; &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&lt;!–&nbsp;Swagger&nbsp;API文档&nbsp;–&gt; &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&lt;dependency&gt; &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&lt;groupId&gt;io.springfox&lt;/groupId&gt; &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&lt;artifactId&gt;springfox-swagger-ui&lt;/artifactId&gt; &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&lt;version&gt;2.2.2&lt;/version&gt; &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&lt;/dependency&gt; &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&lt;dependency&gt; &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&lt;groupId&gt;io.springfox&lt;/groupId&gt; &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&lt;artifactId&gt;springfox-swagger2&lt;/artifactId&gt; &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&lt;version&gt;2.2.2&lt;/version&gt; &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&lt;/dependency&gt; &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&lt;!–&nbsp;Mybatis&nbsp;–&gt; &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&lt;dependency&gt; &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&lt;groupId&gt;org.mybatis.spring.boot&lt;/groupId&gt; &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&lt;artifactId&gt;mybatis-spring-boot-starter&lt;/artifactId&gt; &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&lt;version&gt;1.1.1&lt;/version&gt; &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&lt;/dependency&gt; &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&lt;!–&nbsp;druid阿里巴巴数据库连接池&nbsp;–&gt; &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&lt;dependency&gt; &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&lt;groupId&gt;com.alibaba&lt;/groupId&gt; &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&lt;artifactId&gt;druid&lt;/artifactId&gt; &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&lt;version&gt;1.0.26&lt;/version&gt; &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&lt;/dependency&gt; &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&lt;!–&nbsp;MySql数据库驱动&nbsp;–&gt; &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&lt;dependency&gt; &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&lt;groupId&gt;&nbsp;mysql&lt;/groupId&gt; &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&lt;artifactId&gt;&nbsp;mysql-connector-java&lt;/artifactId&gt; &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&lt;version&gt;&nbsp;5.0.5&lt;/version&gt; &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&lt;/dependency&gt;</pre><p style="">其中spring-boot-devtools不是必须,只是如果你想在运行的时候,修改了代码能自动更新,而不用手动重启,就需要加上</p><h5 style="">三、添加Applcation类</h5><p style="">这个就是程序的入口类了,代码如下</p><p>Java</p><pre class=" language-java" style="font-family:Consolas,Monaco,&#39;Andale Mono&#39;,&#39;Ubuntu Mono&#39;,monospace; font-size:0.9em; margin-top:0.5em; margin-bottom:0.5em; border:1px solid rgb(227,237,243); width:1178px; padding:1em; overflow:auto; color:rgb(248,248,242); word-spacing:normal; word-break:normal; word-wrap:normal; line-height:1.5; background:rgb(39,40,34)">package&nbsp;wang.raye.springboot; import&nbsp;org.mybatis.spring.annotation.MapperScan; import&nbsp;org.springframework.boot.SpringApplication; import&nbsp;org.springframework.boot.autoconfigure.SpringBootApplication; import&nbsp;org.springframework.boot.web.servlet.ServletComponentScan; import&nbsp;org.springframework.boot.web.support.SpringBootServletInitializer; / &nbsp;* &nbsp;* &nbsp;*&nbsp;@author&nbsp;Raye &nbsp;*&nbsp;@since&nbsp;2016年10月9日22:22:31 &nbsp;*/@MapperScan(&quot;wang.raye.springboot.model.mapper&quot;)@SpringBootApplication@ServletComponentScanpublic&nbsp;class&nbsp;Application&nbsp;extends&nbsp;SpringBootServletInitializer&nbsp;{ &nbsp;&nbsp;&nbsp;&nbsp;public&nbsp;static&nbsp;void&nbsp;main(String[]&nbsp;args)&nbsp;{ &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;SpringApplication.run(Application.class); &nbsp;&nbsp;&nbsp;&nbsp;}}</pre><p style="">想要运行项目时可以直接运行此类就可以,如你所见,其中有main方法,所以可以直接运行</p><p style="">下面说说3个注解的含义</p><p style=""><span style="font-weight:700">@MapperScan(&quot;wang.raye.springboot.model.mapper&quot;)</span>,扫描wang.raye.springboot.model.mapper下面的mapper接口,其中mapper下面的接口是由mybatis-generator自动生成的,会在后面细说,现在就先知道是个什么东西就行了&nbsp;<span style="font-weight:700">@SpringBootApplication</span>很多Spring Boot开发者总是使用 @Configuration , @EnableAutoConfiguration 和 @ComponentScan 注解他们的main类。由于这些注解被如此频繁地一块使用(特别是你遵循以上最佳实践时),Spring Boot提供一个方便的 @SpringBootApplication 选择。该 @SpringBootApplication 注解等价于以默认属性使用 @Configuration , @EnableAutoConfiguration 和 @ComponentScan</p><p style=""><span style="font-weight:700">@ServletComponentScan</span>有此注解后,项目中如果需要使用java原生的servlet和filter,可以在类中使用注解实现,主要是配置Druid监控时需要用到</p><h5 style="">四、创建配置文件</h5><p style="">虽然spring boot可以不用配置xml,但是也并不是完全不需要配置文件,当然不用配置文件也能跑起来,只是有配置文件我们可以配置很多东西,只是不用像以前xml那么麻烦,首先需要在resource文件夹下面创建application.yml文件</p><pre class=" language-yml" style="font-family:Consolas,Monaco,&#39;Andale Mono&#39;,&#39;Ubuntu Mono&#39;,monospace; font-size:0.9em; margin-top:0.5em; margin-bottom:0.5em; border:1px solid rgb(227,237,243); width:1178px; padding:1em; overflow:auto; color:rgb(248,248,242); word-spacing:normal; word-break:normal; word-wrap:normal; line-height:1.5; background:rgb(39,40,34)">server:&nbsp;&nbsp; &nbsp;&nbsp;port:&nbsp;80 spring:&nbsp;&nbsp; &nbsp;&nbsp;application: &nbsp;&nbsp;&nbsp;&nbsp;name:&nbsp;admin-managee &nbsp;&nbsp;datasource: &nbsp;&nbsp;&nbsp;&nbsp;url:&nbsp;jdbc:mysql:192.168.157.133:3306/springboot &nbsp;&nbsp;&nbsp;&nbsp;username:&nbsp;raye &nbsp;&nbsp;&nbsp;&nbsp;password:&nbsp;123456 &nbsp;&nbsp;&nbsp;&nbsp;driver-class-name:&nbsp;com.mysql.jdbc.Driver &nbsp;&nbsp;&nbsp;&nbsp;type:&nbsp;com.alibaba.druid.pool.DruidDataSource &nbsp;&nbsp;&nbsp;&nbsp;druid: &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;max-active:&nbsp;20 &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;initial-size:&nbsp;1 &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;min-idle:&nbsp;3 &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;max-wait:&nbsp;60000 &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;time-between-eviction-runs-millis:&nbsp;60000 &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;min-evictable-idle-time-millis:&nbsp;300000 &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;test-while-idle:&nbsp;true &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;test-on-borrow:&nbsp;false &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;test-on-return:&nbsp;false debug:&nbsp;true</pre><p style="">相信这个文件很容易看懂,首先server:下面的port:80这个是定义了运行的端口,之前说过spring boot内置了tomcat服务器,所以如果要使用内置的tomcat并且不想用8080端口,就需要在这里配置</p><p style="">下面就是datasource的配置,这样不写进代码有个好处,那就是如果是给客户用的程序,可以很方便的修改数据库配置,而不用重新编译,当然如果你是打包jar我估计是要重新编译的,如果是war,那么就可以解压出来直接修改application.yml而不用重新编译了</p><p style="">而debug:true就是说明当时是调试状态,这样就会输出很多log</p><h5 style="">五、创建Druid数据源配置类</h5><p>Java</p><pre class=" language-java" style="font-family:Consolas,Monaco,&#39;Andale Mono&#39;,&#39;Ubuntu Mono&#39;,monospace; font-size:0.9em; margin-top:0.5em; margin-bottom:0.5em; border:1px solid rgb(227,237,243); width:1178px; padding:1em; overflow:auto; color:rgb(248,248,242); word-spacing:normal; word-break:normal; word-wrap:normal; line-height:1.5; background:rgb(39,40,34)">package&nbsp;wang.raye.springboot.config;import&nbsp;java.sql.SQLException; import&nbsp;javax.sql.DataSource;import&nbsp;org.springframework.boot.bind.RelaxedPropertyResolver; import&nbsp;org.springframework.context.EnvironmentAware; import&nbsp;org.springframework.context.annotation.Bean; import&nbsp;org.springframework.context.annotation.Configuration; import&nbsp;org.springframework.core.env.Environment; import&nbsp;org.springframework.transaction.annotation.EnableTransactionManagement; import&nbsp;com.alibaba.druid.pool.DruidDataSource;@Configuration@EnableTransactionManagement / &nbsp;*&nbsp;Druid的DataResource配置类 &nbsp;*&nbsp;@author&nbsp;Raye &nbsp;*&nbsp;@since&nbsp;2016年10月7日14:14:18 &nbsp;*/public&nbsp;class&nbsp;DruidDataSourceConfig&nbsp;&nbsp;implements&nbsp;EnvironmentAware&nbsp;{ &nbsp;&nbsp;&nbsp;&nbsp;private&nbsp;RelaxedPropertyResolver&nbsp;propertyResolver; &nbsp;&nbsp;&nbsp;&nbsp;public&nbsp;void&nbsp;setEnvironment(Environment&nbsp;env)&nbsp;{ &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;this.propertyResolver&nbsp;=&nbsp;new&nbsp;RelaxedPropertyResolver(env,&nbsp;&quot;spring.datasource.&quot;); &nbsp;&nbsp;&nbsp;&nbsp;} &nbsp;&nbsp;&nbsp;&nbsp;@Bean &nbsp;&nbsp;&nbsp;&nbsp;public&nbsp;DataSource&nbsp;dataSource()&nbsp;{ &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;System.out.println(&quot;注入druid!!!&quot;); &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;DruidDataSource&nbsp;datasource&nbsp;=&nbsp;new&nbsp;DruidDataSource(); &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;datasource.setUrl(propertyResolver.getProperty(&quot;url&quot;)); &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;datasource.setDriverClassName(propertyResolver.getProperty(&quot;driver-class-name&quot;)); &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;datasource.setUsername(propertyResolver.getProperty(&quot;username&quot;)); &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;datasource.setPassword(propertyResolver.getProperty(&quot;password&quot;)); &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;datasource.setInitialSize(Integer.valueOf(propertyResolver.getProperty(&quot;initial-size&quot;))); &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;datasource.setMinIdle(Integer.valueOf(propertyResolver.getProperty(&quot;min-idle&quot;))); &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;datasource.setMaxWait(Long.valueOf(propertyResolver.getProperty(&quot;max-wait&quot;))); &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;datasource.setMaxActive(Integer.valueOf(propertyResolver.getProperty(&quot;max-active&quot;))); &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;datasource.setMinEvictableIdleTimeMillis(Long.valueOf(propertyResolver.getProperty(&quot;min-evictable-idle-time-millis&quot;))); &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;try&nbsp;{ &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;datasource.setFilters(&quot;stat,wall&quot;); &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;}&nbsp;catch&nbsp;(SQLException&nbsp;e)&nbsp;{ &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;e.printStackTrace(); &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;} &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;return&nbsp;datasource; &nbsp;&nbsp;&nbsp;&nbsp;}}</pre><p style="">主要是创建一个druid的DruidDataSource 返回并告诉spring boot这是一个bean</p><h5 style="">六、创建Druid的监控servlet和filter</h5><p style=""><span style="font-weight:700">创建监控Servlet</span></p><p>Java</p><pre class=" language-java" style="font-family:Consolas,Monaco,&#39;Andale Mono&#39;,&#39;Ubuntu Mono&#39;,monospace; font-size:0.9em; margin-top:0.5em; margin-bottom:0.5em; border:1px solid rgb(227,237,243); width:1178px; padding:1em; overflow:auto; color:rgb(248,248,242); word-spacing:normal; word-break:normal; word-wrap:normal; line-height:1.5; background:rgb(39,40,34)">package&nbsp;wang.raye.springboot.config; import&nbsp;javax.servlet.annotation.WebInitParam; import&nbsp;javax.servlet.annotation.WebServlet; import&nbsp;com.alibaba.druid.support.http.StatViewServlet; / &nbsp;*&nbsp;Druid的Servlet &nbsp;*&nbsp;@author&nbsp;Raye &nbsp;*&nbsp;@since&nbsp;2016年10月7日14:13:39 &nbsp;*/@SuppressWarnings(&quot;serial&quot;)@WebServlet(urlPatterns&nbsp;=&nbsp;&quot;/druid/*&quot;,initParams={ &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;@WebInitParam(name=&quot;allow&quot;,value=&quot;127.0.0.1,192.168.1.126&quot;),&nbsp;IP白名单&nbsp;(没有配置或者为空,则允许所有访问) &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;@WebInitParam(name=&quot;deny&quot;,value=&quot;192.168.1.111&quot;),&nbsp;IP黑名单&nbsp;(存在共同时,deny优先于allow) &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;@WebInitParam(name=&quot;loginUsername&quot;,value=&quot;Raye&quot;),&nbsp;用户名 &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;@WebInitParam(name=&quot;loginPassword&quot;,value=&quot;123456&quot;),&nbsp;密码 &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;@WebInitParam(name=&quot;resetEnable&quot;,value=&quot;false&quot;)&nbsp;禁用HTML页面上的“Reset&nbsp;All”功能}) &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;public&nbsp;class&nbsp;DruidStatViewServlet&nbsp;extends&nbsp;StatViewServlet&nbsp;{}</pre><p style=""><span style="font-weight:700">@WebServlet</span>表明这是一个Servlet,和之前Application中的@ServletComponentScan相对应使用&nbsp;<span style="font-weight:700">创建filter</span></p><p>Java</p><pre class=" language-java" style="font-family:Consolas,Monaco,&#39;Andale Mono&#39;,&#39;Ubuntu Mono&#39;,monospace; font-size:0.9em; margin-top:0.5em; margin-bottom:0.5em; border:1px solid rgb(227,237,243); width:1178px; padding:1em; overflow:auto; color:rgb(248,248,242); word-spacing:normal; word-break:normal; word-wrap:normal; line-height:1.5; background:rgb(39,40,34)">package&nbsp;wang.raye.springboot.config;import&nbsp;javax.servlet.annotation.WebFilter;import&nbsp;javax.servlet.annotation.WebInitParam;import&nbsp;com.alibaba.druid.support.http.WebStatFilter;@WebFilter(filterName=&quot;druidWebStatFilter&quot;,urlPatterns=&quot;/*&quot;,initParams={ &nbsp;&nbsp;&nbsp;&nbsp;@WebInitParam(name=&quot;exclusions&quot;,value=&quot;*.js,*.gif,*.jpg,*.bmp,*.png,*.css,*.ico,/druid/*&quot;)&nbsp;忽略资源})/ &nbsp;*&nbsp;Druid拦截器,用于查看Druid监控&nbsp;*&nbsp;@author&nbsp;Raye&nbsp;*&nbsp;@since&nbsp;2016年10月7日14:59:27 &nbsp;*/public&nbsp;class&nbsp;DruidStatFilter&nbsp;extends&nbsp;WebStatFilter&nbsp;{}</pre><p style="">同样<span style="font-weight:700">@WebFilter</span>表明此类是一个拦截器</p><p style="">创建好之后,我们访问<a target="_blank" href="http://localhost/druid/index.html,%E4%BC%9A%E8%87%AA%E5%8A%A8%E8%B7%B3%E5%88%B0http://localhost/druid/login.html%E7%99%BB%E5%BD%95%E9%A1%B5%E9%9D%A2%EF%BC%8C%E7%99%BB%E5%BD%95%E8%BF%9B%E5%8E%BB%E4%BC%9A%E5%87%BA%E7%8E%B0%E4%BB%A5%E4%B8%8B%E7%95%8C%E9%9D%A2" style="color:rgb(74,74,74); background:transparent">http://localhost/druid/index.html,会自动跳到http://localhost/druid/login.html登录页面,登录进去会出现以下界面</a><img src="https://img-blog.csdn.net/20160318172740791" alt="预览图"/></p><h5 style="">七、配置mybatis</h5><p style="">其实mybatis之前就已经配置好了,就是<span style="font-weight:700">Application</span>类的<span style="font-weight:700">@MapperScan(&quot;wang.raye.springboot.model.mapper&quot;)</span>注解,这个注解说明了要扫描的mybatis的mapper接口包,当然如果是用xml的话应该还需要其他配置,不过我个人并不喜欢用xml的方式,所以也没有怎么研究</p><h5 style="">八、配置mybatis-generator</h5><p style="">配置MyBatis-generator自动生成实体bean,首先需要在pom.xml中添加相关插件依赖,注:我是用插件的方式来生成实体bean的,</p><p>XML</p><pre class=" language-xml" style="font-family:Consolas,Monaco,&#39;Andale Mono&#39;,&#39;Ubuntu Mono&#39;,monospace; font-size:0.9em; margin-top:0.5em; margin-bottom:0.5em; border:1px solid rgb(227,237,243); width:1178px; padding:1em; overflow:auto; color:rgb(248,248,242); word-spacing:normal; word-break:normal; word-wrap:normal; line-height:1.5; background:rgb(39,40,34)">&lt;build&gt; &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&lt;finalName&gt;springboot&lt;/finalName&gt; &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&lt;plugins&gt; &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&lt;plugin&gt; &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&lt;groupId&gt;org.mybatis.generator&lt;/groupId&gt; &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&lt;artifactId&gt;mybatis-generator-maven-plugin&lt;/artifactId&gt; &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&lt;version&gt;1.3.5&lt;/version&gt; &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&lt;dependencies&gt; &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&lt;!–数据库驱动&nbsp;–&gt; &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&lt;dependency&gt; &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&lt;groupId&gt;&nbsp;mysql&lt;/groupId&gt; &nbsp;&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bsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&lt;overwrite&gt;true&lt;/overwrite&gt; &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&lt;!–&nbsp;自动生成的配置&nbsp;–&gt; &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&lt;configurationFile&gt; &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;src/main/resources/mybatis-generator.xml&lt;/configurationFile&gt; &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&lt;/configuration&gt; &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&lt;/plugin&gt; &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&lt;plugin&gt; &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&lt;artifactId&gt;maven-compiler-plugin&lt;/artifactId&gt; &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&lt;configuration&gt; &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&lt;encoding&gt;UTF-8&lt;/encoding&gt; &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&lt;source&gt;1.5&lt;/source&gt; &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&lt;target&gt;1.5&lt;/target&gt; &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&lt;/configuration&gt; &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&lt;/plugin&gt; &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&lt;/plugins&gt; &nbsp;&nbsp;&nbsp;&nbsp;&lt;/build&gt;</pre><p style="">其中除了springboot是pom.xml默认的节点外,其他都是配置Mybatis-generator的,当然还有顶部的</p><p>XML</p><pre class=" language-xml" style="font-family:Consolas,Monaco,&#39;Andale Mono&#39;,&#39;Ubuntu Mono&#39;,monospace; font-size:0.9em; margin-top:0.5em; margin-bottom:0.5em; border:1px solid rgb(227,237,243); width:1178px; padding:1em; overflow:auto; color:rgb(248,248,242); word-spacing:normal; word-break:normal; word-wrap:normal; line-height:1.5; background:rgb(39,40,34)">&lt;properties&gt; &nbsp;&nbsp;&nbsp;&nbsp;&lt;project.build.sourceEncoding&gt;UTF-8&lt;/project.build.sourceEncoding&gt; &nbsp;&nbsp;&nbsp;&nbsp;&lt;entity.target.dir&gt;src/main/java/&lt;/entity.target.dir&gt; &nbsp;&nbsp;&nbsp;&nbsp;&lt;dao.resources.dir&gt;src/main/resources/&lt;/dao.resources.dir&gt;&lt;/properties&gt;</pre><p style="">这个是用来在<span style="font-weight:700">mybatis-generator.xml</span>中需要使用的</p><p style=""><span style="font-weight:700">下面开始配置mybatis-generator.xml</span>&nbsp;首先在resource文件夹下面创建mybatis-generator.xml文件,然后添加如下配置</p><p>XML</p><pre class=" language-xml" style="font-family:Consolas,Monaco,&#39;Andale Mono&#39;,&#39;Ubuntu Mono&#39;,monospace; font-size:0.9em; margin-top:0.5em; margin-bottom:0.5em; border:1px solid rgb(227,237,243); width:1178px; padding:1em; overflow:auto; color:rgb(248,248,242); word-spacing:normal; word-break:normal; word-wrap:normal; line-height:1.5; background:rgb(39,40,34)">&lt;?xml&nbsp;version=&quot;1.0&quot;&nbsp;encoding=&quot;UTF-8&quot;?&gt;&lt;!DOCTYPE&nbsp;generatorConfiguration &nbsp;&nbsp;PUBLIC&nbsp;&quot;-mybatis.orgDTD&nbsp;MyBatis&nbsp;Generator&nbsp;Configuration&nbsp;1.0EN&quot; &nbsp;&nbsp;&quot;http://mybatis.org/dtd/mybatis-generator-config_1_0.dtd"><generatorConfiguration> &nbsp;&nbsp;&nbsp;&nbsp;&lt;context&nbsp;id=&quot;DB2Tables&quot;&nbsp;targetRuntime=&quot;MyBatis3&quot;&nbsp;defaultModelType=&quot;flat&quot;&gt; &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&lt;plugin&nbsp;type=&quot;org.mybatis.generator.plugins.RenameExampleClassPlugin&quot;&gt; &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&lt;property&nbsp;name=&quot;searchString&quot;&nbsp;value=&quot;[e|E]xample$&quot;&nbsp;/&gt; &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&lt;property&nbsp;name=&quot;replaceString&quot;&nbsp;value=&quot;Criteria&quot;&nbsp;/&gt; &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&lt;/plugin&gt; &nbsp;&nbsp;&nbsp;&nbsp;&lt;commentGenerator&gt; &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&lt;property&nbsp;name=&quot;suppressDate&quot;&nbsp;value=&quot;true&quot;&nbsp;/&gt; &nbsp;&nbsp;&nbsp;&nbsp;&lt;/commentGenerator&gt; &nbsp;&nbsp;&nbsp;&nbsp;&lt;jdbcConnection&nbsp;driverClass=&quot;com.mysql.jdbc.Driver&quot; &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;connectionURL=&quot;jdbc:mysql:192.168.157.133:3306/springboot&quot;&nbsp;userId=&quot;root&quot; &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;password=&quot;1993316&quot;&gt; &nbsp;&nbsp;&nbsp;&nbsp;&lt;/jdbcConnection&gt; &nbsp;&nbsp;&nbsp;&nbsp;&lt;javaTypeResolver&gt; &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&lt;property&nbsp;name=&quot;forceBigDecimals&quot;&nbsp;value=&quot;false&quot;&nbsp;/&gt; &nbsp;&nbsp;&nbsp;&nbsp;&lt;/javaTypeResolver&gt; &nbsp;&nbsp;&nbsp;&nbsp;&lt;!–&nbsp;generate&nbsp;Model&nbsp;–&gt; &nbsp;&nbsp;&nbsp;&nbsp;&lt;javaModelGenerator&nbsp;targetPackage=&quot;wang.raye.springboot.model&quot; &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;targetProject=&quot;${entity.target.dir}&quot;&gt; &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&lt;property&nbsp;name=&quot;enableSubPackages&quot;&nbsp;value=&quot;true&quot;&nbsp;/&gt; &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&lt;property&nbsp;name=&quot;trimStrings&quot;&nbsp;value=&quot;true&quot;&nbsp;/&gt; &nbsp;&nbsp;&nbsp;&nbsp;&lt;/javaModelGenerator&gt; &nbsp;&nbsp;&nbsp;&nbsp;&lt;!–&nbsp;自动生成xml(但是好像一直没有生成功,反正也不用xml)&nbsp;–&gt; &nbsp;&nbsp;&nbsp;&nbsp;&lt;sqlMapGenerator&nbsp;&nbsp;targetPackage=&quot;wang.raye.springboot.model.mapper&quot; &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;targetProject=&quot;${dao.resources.dir}&quot;&gt; &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&lt;property&nbsp;name=&quot;enableSubPackages&quot;&nbsp;value=&quot;true&quot;&nbsp;/&gt; &nbsp;&nbsp;&nbsp;&nbsp;&lt;/sqlMapGenerator&gt; &nbsp;&nbsp;&nbsp;&nbsp;&lt;!–&nbsp;自动生成mapper接口,&nbsp;可以是&nbsp;ANNOTATEDMAPPER(注解),&nbsp;XMLMAPPER(xml),&nbsp;MIXEDMAPPER(不知道)&nbsp;–&gt; &nbsp;&nbsp;&nbsp;&nbsp;&lt;javaClientGenerator&nbsp;type=&quot;ANNOTATEDMAPPER&quot; &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;targetPackage=&quot;wang.raye.springboot.model.mapper&quot;&nbsp;implementationPackage=&quot;wang.raye.springboot.model.mapper.impl&quot; &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;targetProject=&quot;${entity.target.dir}&quot;&gt; &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&lt;property&nbsp;name=&quot;enableSubPackages&quot;&nbsp;value=&quot;true&quot;&nbsp;/&gt; &nbsp;&nbsp;&nbsp;&nbsp;&lt;/javaClientGenerator&gt; &nbsp;&nbsp;&nbsp;&nbsp;&lt;table&nbsp;tableName=&quot;user&quot;&nbsp;domainObjectName=&quot;User&quot;&nbsp;&gt;&lt;/table&gt; &nbsp;&nbsp;&nbsp;&nbsp;&lt;/context&gt;&lt;/generatorConfiguration&gt;</pre><p style="">其实需要自己修改数据库配置,因为插件不会读取spring boot中的数据库配置,所以需要在mybatis-generator.xml中配置好数据库,同时也需要修改自己的包名</p><p style=""><span style="font-weight:700">table</span>节点中的tableName是指在数据库中的表名,domainObjectName是只生成的实体bean的类名,当然domainObjectName可以不用配置,会默认生成,当然如果有特殊需要可以配置</p><h5 style="">配置Swagger2</h5><p>Java</p><pre class=" language-java" style="font-family:Consolas,Monaco,&#39;Andale Mono&#39;,&#39;Ubuntu Mono&#39;,monospace; font-size:0.9em; margin-top:0.5em; margin-bottom:0.5em; border:1px solid rgb(227,237,243); width:1178px; padding:1em; overflow:auto; color:rgb(248,248,242); word-spacing:normal; word-break:normal; word-wrap:normal; line-height:1.5; background:rgb(39,40,34)">package&nbsp;wang.raye.springboot;import&nbsp;org.springframework.context.annotation.Bean;import&nbsp;org.springframework.context.annotation.Configuration;import&nbsp;springfox.documentation.builders.PathSelectors;import&nbsp;springfox.documentation.builders.RequestHandlerSelectors;import&nbsp;springfox.documentation.service.ApiInfo;import&nbsp;springfox.documentation.spi.DocumentationType;import&nbsp;springfox.documentation.spring.web.plugins.Docket;import&nbsp;springfox.documentation.swagger2.annotations.EnableSwagger2;import&nbsp;wang.raye.springboot.model.User;/ &nbsp;*&nbsp;SwaggerConfig &nbsp;*/@Configuration@EnableSwagger2public&nbsp;class&nbsp;SwaggerConfig&nbsp;{ &nbsp;&nbsp;&nbsp;&nbsp;/ &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;*&nbsp;可以定义多个组,比如本类中定义把test和demo区分开了&nbsp;(访问页面就可以看到效果了) &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;* &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;*/ &nbsp;&nbsp;&nbsp;&nbsp;@Bean &nbsp;&nbsp;&nbsp;&nbsp;public&nbsp;Docket&nbsp;testApi()&nbsp;{ &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;return&nbsp;new&nbsp;Docket(DocumentationType.SWAGGER_2) &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;.apiInfo(apiInfo()) &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;.select() &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;.apis(RequestHandlerSelectors.basePackage(&quot;wang.raye.springboot&quot;)) &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;.paths(PathSelectors.any()).build(); &nbsp;&nbsp;&nbsp;&nbsp;} &nbsp;&nbsp;&nbsp;&nbsp;private&nbsp;ApiInfo&nbsp;apiInfo()&nbsp;{ &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;ApiInfo&nbsp;apiInfo&nbsp;=&nbsp;new&nbsp;ApiInfo(&quot;SpringBoot学习demo&quot;,&nbsp;&nbsp;大标题 &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&quot;Spring&nbsp;boot&nbsp;+&nbsp;swagger&nbsp;+&nbsp;mybatis&nbsp;+&nbsp;druid&quot;,&nbsp;&nbsp;小标题 &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&quot;1.0&quot;,&nbsp;&nbsp;版本 &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&quot;NO&nbsp;terms&nbsp;of&nbsp;service&quot;,&nbsp;&quot;admin@raye.wang&quot;,&nbsp;&nbsp;作者 &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&quot;RayeBlog&quot;,&nbsp;&nbsp;链接显示文字 &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&quot;http://www.raye.wang/"// 网站链接 &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;); &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;return&nbsp;apiInfo; &nbsp;&nbsp;&nbsp;&nbsp;}}</pre><p style="">相信看代码很容易看懂,我就不多说了</p><h4 style="">编写demo</h4><p style="">至此,环境配置都配置好了,然后我创建一个简单的接口来添加用户,首先看看我的表结构</p><p>SQL</p><pre class=" language-sql" style="font-family:Consolas,Monaco,&#39;Andale Mono&#39;,&#39;Ubuntu Mono&#39;,monospace; font-size:0.9em; margin-top:0.5em; margin-bottom:0.5em; border:1px solid rgb(227,237,243); width:1178px; padding:1em; overflow:auto; color:rgb(248,248,242); word-spacing:normal; word-break:normal; word-wrap:normal; line-height:1.5; background:rgb(39,40,34)">DROP&nbsp;TABLE&nbsp;IF&nbsp;EXISTS&nbsp;`user`;CREATE&nbsp;TABLE&nbsp;`user`&nbsp;( &nbsp;&nbsp;`id`&nbsp;int(11)&nbsp;NOT&nbsp;NULL&nbsp;AUTO_INCREMENT, &nbsp;&nbsp;`username`&nbsp;varchar(30)&nbsp;NOT&nbsp;NULL, &nbsp;&nbsp;`psw`&nbsp;varchar(40)&nbsp;NOT&nbsp;NULL, &nbsp;&nbsp;PRIMARY&nbsp;KEY&nbsp;(`id`))&nbsp;ENGINE=InnoDB&nbsp;AUTO_INCREMENT=0&nbsp;DEFAULT&nbsp;CHARSET=utf8;</pre><p style="">因为只是做演示,所以非常简单,然后看我生成的User.java</p><p>Java</p><pre class=" language-java" style="font-family:Consolas,Monaco,&#39;Andale Mono&#39;,&#39;Ubuntu Mono&#39;,monospace; font-size:0.9em; margin-top:0.5em; margin-bottom:0.5em; border:1px solid rgb(227,237,243); width:1178px; padding:1em; overflow:auto; color:rgb(248,248,242); word-spacing:normal; word-break:normal; word-wrap:normal; line-height:1.5; background:rgb(39,40,34)">package&nbsp;wang.raye.springboot.model;public&nbsp;class&nbsp;User&nbsp;{ &nbsp;&nbsp;&nbsp;&nbsp;private&nbsp;Integer&nbsp;id; &nbsp;&nbsp;&nbsp;&nbsp;private&nbsp;String&nbsp;username; &nbsp;&nbsp;&nbsp;&nbsp;private&nbsp;String&nbsp;psw; &nbsp;&nbsp;&nbsp;&nbsp;public&nbsp;Integer&nbsp;getId()&nbsp;{ &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;return&nbsp;id; &nbsp;&nbsp;&nbsp;&nbsp;} &nbsp;&nbsp;&nbsp;&nbsp;public&nbsp;void&nbsp;setId(Integer&nbsp;id)&nbsp;{ &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;this.id&nbsp;=&nbsp;id; &nbsp;&nbsp;&nbsp;&nbsp;} &nbsp;&nbsp;&nbsp;&nbsp;public&nbsp;String&nbsp;getUsername()&nbsp;{ &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;return&nbsp;username; &nbsp;&nbsp;&nbsp;&nbsp;} &nbsp;&nbsp;&nbsp;&nbsp;public&nbsp;void&nbsp;setUsername(String&nbsp;username)&nbsp;{ &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;this.username&nbsp;=&nbsp;username&nbsp;==&nbsp;null&nbsp;?&nbsp;null&nbsp;:&nbsp;username.trim(); &nbsp;&nbsp;&nbsp;&nbsp;} &nbsp;&nbsp;&nbsp;&nbsp;public&nbsp;String&nbsp;getPsw()&nbsp;{ &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;return&nbsp;psw; &nbsp;&nbsp;&nbsp;&nbsp;} &nbsp;&nbsp;&nbsp;&nbsp;public&nbsp;void&nbsp;setPsw(String&nbsp;psw)&nbsp;{ &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;this.psw&nbsp;=&nbsp;psw&nbsp;==&nbsp;null&nbsp;?&nbsp;null&nbsp;:&nbsp;psw.trim(); &nbsp;&nbsp;&nbsp;&nbsp;}}</pre><p style="">当然我是删除了很多自动生成的注释,因为看着太烦,不适合在博客上展示,然后看看UserMapper.java</p><p>Java</p><pre class=" language-java" style="font-family:Consolas,Monaco,&#39;Andale Mono&#39;,&#39;Ubuntu Mono&#39;,monospace; font-size:0.9em; margin-top:0.5em; margin-bottom:0.5em; border:1px solid rgb(227,237,243); width:1178px; padding:1em; overflow:auto; color:rgb(248,248,242); word-spacing:normal; word-break:normal; word-wrap:normal; line-height:1.5; background:rgb(39,40,34)">package&nbsp;wang.raye.springboot.model.mapper;import&nbsp;java.util.List;import&nbsp;org.apache.ibatis.annotations.Delete;import&nbsp;org.apache.ibatis.annotations.DeleteProvider;import&nbsp;org.apache.ibatis.annotations.Insert;import&nbsp;org.apache.ibatis.annotations.InsertProvider;import&nbsp;org.apache.ibatis.annotations.Param;import&nbsp;org.apache.ibatis.annotations.Result;import&nbsp;org.apache.ibatis.annotations.Results;import&nbsp;org.apache.ibatis.annotations.Select;import&nbsp;org.apache.ibatis.annotations.SelectProvider;import&nbsp;org.apache.ibatis.annotations.Update;import&nbsp;org.apache.ibatis.annotations.UpdateProvider;import&nbsp;org.apache.ibatis.type.JdbcType;import&nbsp;wang.raye.springboot.model.User;import&nbsp;wang.raye.springboot.model.UserCriteria;public&nbsp;interface&nbsp;UserMapper&nbsp;{ &nbsp;&nbsp;&nbsp;&nbsp;@SelectProvider(type=UserSqlProvider.class,&nbsp;method=&quot;countByExample&quot;) &nbsp;&nbsp;&nbsp;&nbsp;long&nbsp;countByExample(UserCriteria&nbsp;example); &nbsp;&nbsp;&nbsp;&nbsp;@DeleteProvider(type=UserSqlProvider.class,&nbsp;method=&quot;deleteByExample&quot;) &nbsp;&nbsp;&nbsp;&nbsp;int&nbsp;deleteByExample(UserCriteria&nbsp;example); &nbsp;&nbsp;&nbsp;&nbsp;@Delete({ &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&quot;delete&nbsp;from&nbsp;user&quot;, &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&quot;where&nbsp;id&nbsp;=&nbsp;#{id,jdbcType=INTEGER}&quot; &nbsp;&nbsp;&nbsp;&nbsp;}) &nbsp;&nbsp;&nbsp;&nbsp;int&nbsp;deleteByPrimaryKey(Integer&nbsp;id); &nbsp;&nbsp;&nbsp;&nbsp;@Insert({ &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&quot;insert&nbsp;into&nbsp;user&nbsp;(id,&nbsp;username,&nbsp;&quot;, &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&quot;psw)&quot;, &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&quot;values&nbsp;(#{id,jdbcType=INTEGER},&nbsp;#{username,jdbcType=VARCHAR},&nbsp;&quot;, &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&quot;#{psw,jdbcType=VARCHAR})&quot; &nbsp;&nbsp;&nbsp;&nbsp;}) &nbsp;&nbsp;&nbsp;&nbsp;int&nbsp;insert(User&nbsp;record); &nbsp;&nbsp;&nbsp;&nbsp;@InsertProvider(type=UserSqlProvider.class,&nbsp;method=&quot;insertSelective&quot;) &nbsp;&nbsp;&nbsp;&nbsp;int&nbsp;insertSelective(User&nbsp;record); &nbsp;&nbsp;&nbsp;&nbsp;@SelectProvider(type=UserSqlProvider.class,&nbsp;method=&quot;selectByExample&quot;) &nbsp;&nbsp;&nbsp;&nbsp;@Results({ &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;@Result(column=&quot;id&quot;,&nbsp;property=&quot;id&quot;,&nbsp;jdbcType=JdbcType.INTEGER,&nbsp;id=true), &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;@Result(column=&quot;username&quot;,&nbsp;property=&quot;username&quot;,&nbsp;jdbcType=JdbcType.VARCHAR), &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;@Result(column=&quot;psw&quot;,&nbsp;property=&quot;psw&quot;,&nbsp;jdbcType=JdbcType.VARCHAR) &nbsp;&nbsp;&nbsp;&nbsp;}) &nbsp;&nbsp;&nbsp;&nbsp;List&lt;User&gt;&nbsp;selectByExample(UserCriteria&nbsp;example); &nbsp;&nbsp;&nbsp;&nbsp;@Select({ &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&quot;select&quot;, &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&quot;id,&nbsp;username,&nbsp;psw&quot;, &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&quot;from&nbsp;user&quot;, &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&quot;where&nbsp;id&nbsp;=&nbsp;#{id,jdbcType=INTEGER}&quot; &nbsp;&nbsp;&nbsp;&nbsp;}) &nbsp;&nbsp;&nbsp;&nbsp;@Results({ &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;@Result(column=&quot;id&quot;,&nbsp;property=&quot;id&quot;,&nbsp;jdbcType=JdbcType.INTEGER,&nbsp;id=true), &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;@Result(column=&quot;username&quot;,&nbsp;property=&quot;username&quot;,&nbsp;jdbcType=JdbcType.VARCHAR), &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;@Result(column=&quot;psw&quot;,&nbsp;property=&quot;psw&quot;,&nbsp;jdbcType=JdbcType.VARCHAR) &nbsp;&nbsp;&nbsp;&nbsp;}) &nbsp;&nbsp;&nbsp;&nbsp;User&nbsp;selectByPrimaryKey(Integer&nbsp;id); &nbsp;&nbsp;&nbsp;&nbsp;@UpdateProvider(type=UserSqlProvider.class,&nbsp;method=&quot;updateByExampleSelective&quot;) &nbsp;&nbsp;&nbsp;&nbsp;int&nbsp;updateByExampleSelective(@Param(&quot;record&quot;)&nbsp;User&nbsp;record,&nbsp;@Param(&quot;example&quot;)&nbsp;UserCriteria&nbsp;example); &nbsp;&nbsp;&nbsp;&nbsp;@UpdateProvider(type=UserSqlProvider.class,&nbsp;method=&quot;updateByExample&quot;) &nbsp;&nbsp;&nbsp;&nbsp;int&nbsp;updateByExample(@Param(&quot;record&quot;)&nbsp;User&nbsp;record,&nbsp;@Param(&quot;example&quot;)&nbsp;UserCriteria&nbsp;example); &nbsp;&nbsp;&nbsp;&nbsp;@UpdateProvider(type=UserSqlProvider.class,&nbsp;method=&quot;updateByPrimaryKeySelective&quot;) &nbsp;&nbsp;&nbsp;&nbsp;int&nbsp;updateByPrimaryKeySelective(User&nbsp;record); &nbsp;&nbsp;&nbsp;&nbsp;@Update({ &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&quot;update&nbsp;user&quot;, &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&quot;set&nbsp;username&nbsp;=&nbsp;#{username,jdbcType=VARCHAR},&quot;, &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&quot;psw&nbsp;=&nbsp;#{psw,jdbcType=VARCHAR}&quot;, &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&quot;where&nbsp;id&nbsp;=&nbsp;#{id,jdbcType=INTEGER}&quot; &nbsp;&nbsp;&nbsp;&nbsp;}) &nbsp;&nbsp;&nbsp;&nbsp;int&nbsp;updateByPrimaryKey(User&nbsp;record);}</pre><p style="">同样删除了注释,当然还会自动生成UserCriteria.java 和UserSqlProvider,这2个类主要用于模板查询,用过myBatis应该都知道,就不贴出来了,主要是我们的demo中也不会用到</p><h5 style="">一、创建UserServer接口</h5><p>Java</p><pre class=" language-java" style="font-family:Consolas,Monaco,&#39;Andale Mono&#39;,&#39;Ubuntu Mono&#39;,monospace; font-size:0.9em; margin-top:0.5em; margin-bottom:0.5em; border:1px solid rgb(227,237,243); width:1178px; padding:1em; overflow:auto; color:rgb(248,248,242); word-spacing:normal; word-break:normal; word-wrap:normal; line-height:1.5; background:rgb(39,40,34)">package&nbsp;wang.raye.springboot.server;import&nbsp;java.util.List;import&nbsp;wang.raye.springboot.model.User;/ &nbsp;*&nbsp;用户服务 &nbsp;*&nbsp;@author&nbsp;Raye &nbsp;*&nbsp;@since&nbsp;2016年9月21日20:57:39 &nbsp;*/public&nbsp;interface&nbsp;UserServer&nbsp;{ &nbsp;&nbsp;&nbsp;&nbsp;/ &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;*&nbsp;添加一个用户 &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;*&nbsp;@param&nbsp;user&nbsp;用户对象 &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;*&nbsp;@since&nbsp;2016年9月21日20:58:17 &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;*&nbsp;@return&nbsp;是否添加成功 &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;*/ &nbsp;&nbsp;&nbsp;&nbsp;public&nbsp;boolean&nbsp;add(User&nbsp;user);}</pre><h5 style="">二、创建UserServerImpl</h5><p>Java</p><pre class=" language-java" style="font-family:Consolas,Monaco,&#39;Andale Mono&#39;,&#39;Ubuntu Mono&#39;,monospace; font-size:0.9em; margin-top:0.5em; margin-bottom:0.5em; border:1px solid rgb(227,237,243); width:1178px; padding:1em; overflow:auto; color:rgb(248,248,242); word-spacing:normal; word-break:normal; word-wrap:normal; line-height:1.5; background:rgb(39,40,34)">package&nbsp;wang.raye.springboot.server.impl;import&nbsp;java.util.List;import&nbsp;org.springframework.beans.factory.annotation.Autowired;import&nbsp;org.springframework.stereotype.Repository;import&nbsp;wang.raye.springboot.model.User;import&nbsp;wang.raye.springboot.model.mapper.UserMapper;import&nbsp;wang.raye.springboot.server.UserServer;/ &nbsp;*&nbsp;用户相关数据库操作实现类 &nbsp;*&nbsp;@author&nbsp;Raye &nbsp;*&nbsp;@since&nbsp;2016年10月11日19:29:02 &nbsp;*/@Repositorypublic&nbsp;class&nbsp;UserServerImpl&nbsp;implements&nbsp;UserServer&nbsp;{ &nbsp;&nbsp;&nbsp;&nbsp;@Autowired &nbsp;&nbsp;&nbsp;&nbsp;private&nbsp;UserMapper&nbsp;mapper; &nbsp;&nbsp;&nbsp;&nbsp;public&nbsp;boolean&nbsp;add(User&nbsp;user)&nbsp;{ &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;return&nbsp;mapper.insert(user)&nbsp;&gt;&nbsp;0; &nbsp;&nbsp;&nbsp;&nbsp;} &nbsp;&nbsp;&nbsp;&nbsp;public&nbsp;List&lt;User&gt;&nbsp;findAll()&nbsp;{ &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;return&nbsp;mapper.selectByExample(null); &nbsp;&nbsp;&nbsp;&nbsp;}}</pre><h5 style="">三、创建UserController</h5><p>Java</p><pre class=" language-java" style="font-family:Consolas,Monaco,&#39;Andale Mono&#39;,&#39;Ubuntu Mono&#39;,monospace; font-size:0.9em; margin-top:0.5em; margin-bottom:0.5em; border:1px solid rgb(227,237,243); width:1178px; padding:1em; overflow:auto; color:rgb(248,248,242); word-spacing:normal; word-break:normal; word-wrap:normal; line-height:1.5; background:rgb(39,40,34)">package&nbsp;wang.raye.springboot.controller;import&nbsp;org.springframework.beans.factory.annotation.Autowired;import&nbsp;org.springframework.web.bind.annotation.RequestBody;import&nbsp;org.springframework.web.bind.annotation.RequestMapping;import&nbsp;org.springframework.web.bind.annotation.RestController;import&nbsp;io.swagger.annotations.Api;import&nbsp;io.swagger.annotations.ApiImplicitParam;import&nbsp;io.swagger.annotations.ApiOperation;import&nbsp;wang.raye.springboot.model.User;import&nbsp;wang.raye.springboot.server.UserServer;@Api(value=&quot;用户相关的接口&quot;)@RestController@RequestMapping(&quot;/user&quot;)public&nbsp;class&nbsp;UserController&nbsp;{ &nbsp;&nbsp;&nbsp;&nbsp;@Autowired &nbsp;&nbsp;&nbsp;&nbsp;private&nbsp;UserServer&nbsp;server; &nbsp;&nbsp;&nbsp;&nbsp;/ &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;*&nbsp;添加用户 &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;*&nbsp;@param&nbsp;user&nbsp;用户对象 &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;*&nbsp;@since&nbsp;2016年9月21日21:01:50 &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;*/ &nbsp;&nbsp;&nbsp;&nbsp;@RequestMapping(&quot;/add&quot;) &nbsp;&nbsp;&nbsp;&nbsp;@ApiOperation(notes=&quot;添加用户&quot;,value=&quot;添加一个用户&quot;,httpMethod=&quot;POST&quot;) &nbsp;&nbsp;&nbsp;&nbsp;@ApiImplicitParam(name&nbsp;=&nbsp;&quot;user&quot;,&nbsp;value&nbsp;=&nbsp;&quot;用户详细实体user&quot;,&nbsp;required&nbsp;=&nbsp;true,&nbsp;dataType&nbsp;=&nbsp;&quot;User&quot;) &nbsp;&nbsp;&nbsp;&nbsp;public&nbsp;String&nbsp;add(@RequestBody&nbsp;User&nbsp;user){ &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;return&nbsp;&quot;hello&nbsp;&quot;+server.add(user); &nbsp;&nbsp;&nbsp;&nbsp;}}</pre><p style="">到此一个接口就完成了,我们可以到swagger2的文件页面去测试<a target="_blank" href="http://localhost/swagger-ui.htm" style="color:rgb(74,74,74); background:transparent">http://localhost/swagger-ui.htm</a>&nbsp;注意如果端口不是80需要加上端口号,点开页面中的用户相关的接口 和GET /user/add可以看到如下页面<img src="/dokuwiki/ueditor/php/upload/image/20180614/1528966044173624.png" alt="swagger预览图" class="full-img"/>我们可以在parameters处输入</p><p>JSON</p><pre class=" language-json" style="font-family:Consolas,Monaco,&#39;Andale Mono&#39;,&#39;Ubuntu Mono&#39;,monospace; font-size:0.9em; margin-top:0.5em; margin-bottom:0.5em; border:1px solid rgb(227,237,243); width:1178px; padding:1em; overflow:auto; color:rgb(248,248,242); word-spacing:normal; word-break:normal; word-wrap:normal; line-height:1.5; background:rgb(39,40,34)">{ &nbsp;&nbsp;&quot;psw&quot;:&nbsp;&quot;Raye&quot;, &nbsp;&nbsp;&quot;username&quot;:&nbsp;&quot;123456&quot;}</pre><p style="">点击try it out就可以测试了,当然你也可能不想用json的方式,可以使用</p><p>Java</p><pre class=" language-java" style="font-family:Consolas,Monaco,&#39;Andale Mono&#39;,&#39;Ubuntu Mono&#39;,monospace; font-size:0.9em; margin-top:0.5em; margin-bottom:0.5em; border:1px solid rgb(227,237,243); width:1178px; padding:1em; overflow:auto; color:rgb(248,248,242); word-spacing:normal; word-break:normal; word-wrap:normal; line-height:1.5; background:rgb(39,40,34)">@ApiImplicitParams({ &nbsp;&nbsp;&nbsp;&nbsp;@ApiImplicitParam(name=&quot;username&quot;,paramType=&quot;query&quot;,dataType=&quot;string&quot;), &nbsp;&nbsp;&nbsp;&nbsp;@ApiImplicitParam(name=&quot;psw&quot;,paramType=&quot;query&quot;,dataType=&quot;string&quot;)})</pre><p style="">代替</p><p>Java</p><pre class=" language-java" style="font-family:Consolas,Monaco,&#39;Andale Mono&#39;,&#39;Ubuntu Mono&#39;,monospace; font-size:0.9em; margin-top:0.5em; margin-bottom:0.5em; border:1px solid rgb(227,237,243); width:1178px; padding:1em; overflow:auto; color:rgb(248,248,242); word-spacing:normal; word-break:normal; word-wrap:normal; line-height:1.5; background:rgb(39,40,34)">@ApiImplicitParam(name&nbsp;=&nbsp;&quot;user&quot;,&nbsp;value&nbsp;=&nbsp;&quot;用户详细实体user&quot;,&nbsp;required&nbsp;=&nbsp;true,&nbsp;dataType&nbsp;=&nbsp;&quot;User&quot;)</pre><p style="">同时为了隐藏user参数,需要在SwaggerConfig类的testApi方法中添加.ignoredParameterTypes(User.class)</p><p>Java</p><pre class=" language-java" style="font-family:Consolas,Monaco,&#39;Andale Mono&#39;,&#39;Ubuntu Mono&#39;,monospace; font-size:0.9em; margin-top:0.5em; margin-bottom:0.5em; border:1px solid rgb(227,237,243); width:1178px; padding:1em; overflow:auto; color:rgb(248,248,242); word-spacing:normal; word-break:normal; word-wrap:normal; line-height:1.5; background:rgb(39,40,34)">@Beanpublic&nbsp;Docket&nbsp;testApi()&nbsp;{ &nbsp;&nbsp;&nbsp;&nbsp;return&nbsp;new&nbsp;Docket(DocumentationType.SWAGGER_2) &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;.apiInfo(apiInfo()) &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;.ignoredParameterTypes(User.class) &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;.select() &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;.apis(RequestHandlerSelectors.basePackage(&quot;wang.raye.springboot&quot;)) &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;.paths(PathSelectors.any()).build();}</pre><p style="">测试界面就会变为<img src="/dokuwiki/ueditor/php/upload/image/20180614/1528966045168193.png" alt="swagger2预览图"/></p><h4 style="">结尾</h4><p style="">好了,到这里一个Springboot+Mybatis+Druid+Swagger2+mybatis-generator框架环境就搭建完成了,欢迎大家留言交流,另外附上本项目</p><p style=""><a target="_blank" href="http://git.oschina.net/raywang2014/SpringbootMybatis" style="color:rgb(74,74,74); background:transparent">demo oschina地址</a></p><p style=""><a target="_blank" href="https://github.com/RayeWang/SpringbootMybatis" style="color:rgb(74,74,74); background:transparent">demo github地址</a></p><p><br/></p>