SpringBoot整合Mybatis和JSP完成后台管理项目
环境和工具
开发工具:IDEA
环境:jdk1.8.0_66
总体目录结构
pom.xml 文件
<!--引入SpringBoot内嵌的Tomcat对JSP的解析包-->
<dependency>
<groupId>org.apache.tomcat.embed</groupId>
<artifactId>tomcat-embed-jasper</artifactId>
</dependency>
<!--servlet依赖的jar包-->
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>javax.servlet-api</artifactId>
</dependency>
<!--JSP依赖jar包-->
<dependency>
<groupId>javax.servlet.jsp</groupId>
<artifactId>javax.servlet.jsp-api</artifactId>
<version>2.3.1</version>
</dependency>
<!--jstl标签依赖的jar包-->
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>jstl</artifactId>
</dependency>
<!--SpringBoot整合Mybatis依赖加载-->
<dependency>
<groupId>org.mybatis.spring.boot</groupId>
<artifactId>mybatis-spring-boot-starter</artifactId>
<version>1.3.1</version>
</dependency>
<!--MySQL的jdbc驱动-->
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>5.1.18</version>
</dependency>
<!--springboot开发自动热部署-->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-devtools</artifactId>
<optional>true</optional>
</dependency>
<!-- log4j日志-->
<dependency>
<groupId>log4j</groupId>
<artifactId>log4j</artifactId>
<version>1.2.17</version>
</dependency>
</dependencies>
<build>
<resources>
<resource><!--java下的xml编译到classes文件去-->
<directory>src/main/java</directory>
<includes>
<include>**/*.xml</include>
</includes>
</resource>
<resource><!--所有的配置文件都编译到classes文件夹去-->
<directory>src/main/resources</directory>
<includes>
<include>**/*.*</include>
</includes>
</resource>
<resource><!--webapp下所有资源都编译到META-INF/resources-->
<directory>src/main/webapp</directory>
<targetPath>META-INF/resources</targetPath>
<includes>
<include>**/*.*</include>
</includes>
</resource>
</resources>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
<!--mybatis代码自动生成插件-->
<plugin>
<groupId>org.mybatis.generator</groupId>
<artifactId>mybatis-generator-maven-plugin</artifactId>
<version>1.3.6</version>
<configuration>
<!--配置文件的位置-->
<configurationFile>generatorConfig.xml</configurationFile>
<verbose>true</verbose>
<overwrite>true</overwrite>
</configuration>
</plugin>
</plugins>
</build>
Mybatis 生成基础代码插件配置 generatorConfig.xml
<!-- JDBC数据库驱动jar文件 -->
<classPathEntry location="D:\mysql-connector-java-5.1.18-bin.jar" />
<context id="context1" defaultModelType="flat">
<!-- PO序列化 -->
<plugin type="org.mybatis.generator.plugins.SerializablePlugin">
</plugin>
<commentGenerator>
<!-- 是否去除自动生成的注释 true:是 : false:否 -->
<property name="suppressAllComments" value="true" />
</commentGenerator>
<!-- 配置一个连接 -->
<jdbcConnection
driverClass="com.mysql.jdbc.Driver"
connectionURL="jdbc:mysql://127.0.0.1:3306/数据库名"
userId="用户名"
password="密码">
</jdbcConnection>
<javaTypeResolver>
<property name="forceBigDecimals" value="false" />
</javaTypeResolver>
<!--生成Model类存放位置 -->
<javaModelGenerator targetPackage="com.jy.springboot.pojo" targetProject="src/main/java">
<property name="trimStrings" value="true" />
</javaModelGenerator>
<!--生成映射文件存放位置 -->
<sqlMapGenerator targetPackage="com.jy.springboot.mapper" targetProject="src/main/java">
<property name="trimStrings" value="true" />
</sqlMapGenerator>
<!--生成Dao类存放位置 -->
<javaClientGenerator targetPackage="com.jy.springboot.mapper" targetProject="src/main/java" type="XMLMAPPER" />
<!--tableName 对应数据库表名 -->
<table schema="mybatis" tableName="user" enableCountByExample="false"
enableUpdateByExample="false" enableDeleteByExample="false"
enableSelectByExample="false" selectByExampleQueryId="false">
</table>
</context>
application.properties 文件
#端口号
server.port=8800
#项目上下文
server.servlet.context-path=/myProject
#视图解析器
spring.mvc.view.prefix=/WEB-INF/JSP/
spring.mvc.view.suffix=.jsp
#配置mapper SQL语句的位置
mybatis.mapper-locations=classpath:com/jy/springboot/mapper/*.xml
##数据库链接配置
spring.datasource.username=用户名
spring.datasource.password=密码
spring.datasource.driver-class-name=com.mysql.jdbc.Driver
spring.datasource.url=jdbc:mysql://127.0.0.1:3306/数据库名?useUnicode=true&characterEncoding=utf8
log4j.properties 文件
log4j.rootLogger=DEBUG,CONSOLE,A
log4j.addivity.org.apache=false
log4j.appender.CONSOLE=org.apache.log4j.ConsoleAppender
log4j.appender.CONSOLE.Threshold=DEBUG
log4j.appender.CONSOLE.layout.ConversionPattern=%d{yyyy-MM-dd HH:mm:ss} -%-4r [%t] %-5p %x - %m%n
log4j.appender.CONSOLE.Target=System.out
log4j.appender.CONSOLE.Encoding=gbk
log4j.appender.CONSOLE.layout=org.apache.log4j.PatternLayout
log4j.appender.A=org.apache.log4j.DailyRollingFileAppender
log4j.appender.A.File=${catalina.home}/logs/BSDS_log/PurePro_
log4j.appender.A.DatePattern=yyyy-MM-dd’.log’
log4j.appender.A.layout=org.apache.log4j.PatternLayout
log4j.appender.A.layout.ConversionPattern=[FH_sys] %d{yyyy-MM-dd HH:mm:ss} %5p %c{1}:%L : %m%n