前言
该Demo使用SpringBoot,基于注解方式来开发基于Maven管理的多模块项目。仅仅算是一个简单的模板,方便大家参考学习的同时,也方便自己后续的直接拿来上手。
该项目用到:SpringBoot(2.1.5.RELEASE)、SpringMVC、MyBatis、Druid连接池、Maven、MySQL、Thymeleaf等。开发工具使用的是:Intellij IDEA
项目介绍
项目主要分为5个模块,分别是:①report-dao ②report-pojo ③report-service ④report-utils ⑤report-web
①通过maven<profile>标签,将该项目分为了develop、test、pre-release、release四个环境,方便打包上线时不同环境数据库参数不同而修改的情况;多数据源配置在application.yml中,通过@xxx@来获取<profile>配置的数据
②从架构方向考虑,pom.xml尽量做到了最优。可以通过Maven命令:mvn dependency:tree 来查看依赖关系
③引入MyBatis自动生成代码插件;
④引入多个数据源,通过druid连接池来管理;
⑤*.html后缀管理,目前通过@RequestMapping("xxx.html")来唯一确定缀的,使用yml或者Config.java来配置,虽然满足了后缀管理,但是静态资源无法加载,目前没找到好的办法,后续继续研究,你们有什么方法吗?我们可以一起研讨,谢谢;
⑥SpringBoot不建议使用JSP来开发,推荐使用Thymeleaf、FreeMarker等。本项目配置支持Thymeleaf开发;
⑦项目添加Intellij IDEA热部署支持,满足开发者实时调试。如需配置,可参考:3.Springboot项目在Intellij IDEA中实现热部署
⑧还有AOP,事务,Redis等其他常用的,后续还会陆续加入使用
彼此之间的依赖关系:web依赖service,service依赖dao,dao依赖pojo和until,因为Maven依赖传递,所以web,service等也会间接依赖pojo和until模块。如下为结构图:
POM设计
为了尽量降低各个模块彼此之间的联系,所有父pom使用了<dependencyManagement>来管理项目中使用到的大部分jar包。
如果你不知道该标签是用来干嘛的呢,那么这里就简单来说一下,主要有两点:
①该标签主要从来管理jar包,在父pom中定义的该标签,你会发现好多没用的jar包都在父pom中定义了,太繁琐占空间。切记仅仅只是一个声明罢了,此处并不会使用到这些jar包。
②父pom使用该标签,直接声明版本号。在子pom中使用时,则不需要再声明版本号,仅仅<grupId>和<artifactedId>即可。它会自动使用过父pom使用的版本号。方便以后技术版本的统一变更,这就是为什么要用该标签。
总结:就是方便统一管理。
<!-- 1.父pom -->
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>com.demo</groupId>
<artifactId>bi-report</artifactId>
<version>1.0-SNAPSHOT</version>
<packaging>pom</packaging>
<modules>
<module>report-dao</module>
<module>report-service</module>
<module>report-pojo</module>
<module>report-web</module>
<module>report-utils</module>
</modules>
<!-- 父pom对很多jar都进行了声明,仅仅只是声明而已,方便后续的管理-->
<properties>
<springboot.version>2.1.5.RELEASE</springboot.version>
<springboot.log4j.version>1.3.8.RELEASE</springboot.log4j.version>
<springboot.mybatis.version>1.3.0</springboot.mybatis.version>
<mysql.connector.version>5.1.34</mysql.connector.version>
<druid.version>1.1.9</druid.version>
<junit.version>3.8.1</junit.version>
<nekohtml.version>1.9.18</nekohtml.version>
<commons.lang3.version>3.4</commons.lang3.version>
</properties>
<dependencyManagement>
<dependencies>
<!-- 为什么此处使用spring-boot-dependencies?答:辅助dependencyManagement,方便管理 -->
<!-- 请参考:https://blog.csdn.net/lzb348110175/article/details/90345925 -->
<!-- 添加springboot支持 -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-dependencies</artifactId>
<version>${springboot.version}</version>
<type>pom</type>
<scope>import</scope>
</dependency>
<!-- 添加springboot 热部署依赖 -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-devtools</artifactId>
<version>${springboot.version}</version>
<optional>true</optional>
</dependency>
<!-- springboot整合mybatis -->
<dependency>
<groupId>org.mybatis.spring.boot</groupId>
<artifactId>mybatis-spring-boot-starter</artifactId>
<version>${springboot.mybatis.version}</version>
</dependency>
<!-- mysql驱动 -->
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>${mysql.connector.version}</version>
</dependency>
<!--springboot整合druid-->
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>druid-spring-boot-starter</artifactId>
<version>${druid.version}</version>
</dependency>
<!--springboot整合log4j-->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-log4j</artifactId>
<version>${springboot.log4j.version}</version>
</dependency>
<!--junit测试-->
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>${junit.version}</version>
<scope>test</scope>
</dependency>
<!-- 不严厉H5-->
<dependency>
<groupId>net.sourceforge.nekohtml</groupId>
<artifactId>nekohtml</artifactId>
<version>${nekohtml.version}</version>
</dependency>
<!-- common-lang3工具包-->
<dependency>
<groupId>org.apache.commons</groupId>
<artifactId>commons-lang3</artifactId>
<version>${commons.lang3.version}</version>
</dependency>
</dependencies>
</dependencyManagement>
</project>
<!-- report-web的pom.xml -->
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<parent>
<artifactId>bi-report</artifactId>
<groupId>com.demo</groupId>
<version>1.0-SNAPSHOT</version>
</parent>
<groupId>com.demo</groupId>
<artifactId>report-web</artifactId>
<version>0.0.1-SNAPSHOT</version>
<packaging>war</packaging>
<name>report-web</name>
<description>Divided Module Demo project for Spring Boot</description>
<properties>
<java.version>1.8</java.version>
</properties>
<!-- 如下jar包,在web工程会用到,用不到的一律不依赖,做到最优 -->
<dependencies>
<!--springboot整合web模块-->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
<exclusions> <!-- 排除自带的logging包,换用log4j,做到项目最优-->
<exclusion>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-logging</artifactId>
</exclusion>
</exclusions>
</dependency>
<!-- 单元测试依赖 -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
<!-- web层依赖service-->
<dependency>
<groupId>com.demo</groupId>
<artifactId>report-service</artifactId>
<version>1.0-SNAPSHOT</version>
</dependency>
<!-- springboot添加thymeleaf支持 -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-thymeleaf</artifactId>
</dependency>
<!-- springboot添加热部署支持 -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-devtools</artifactId>
</dependency>
</dependencies>
<!-- profiles标签的使用,方便在开发不同环境之间来回切换 -->
<profiles>
<!-- 开发环境 -->
<profile>
<id>develop</id>
<properties>
<master.driver.className>com.mysql.jdbc.Driver</master.driver.className>
<master.jdbc.url>jdbc:mysql://192.168.91.91:3306/test?useUnicode=true&characterEncoding=UTF-8&allowMultiQueries=true</master.jdbc.url>
<master.username>root</master.username>
<master.password>111111</master.password>
</properties>
<activation>
<activeByDefault>true</activeByDefault>
</activation>
</profile>
<!-- 测试环境 -->
<profile>
<id>test</id>
<properties>
<master.driver.className>com.mysql.jdbc.Driver</master.driver.className>
<master.jdbc.url>jdbc:mysql://192.168.91.92:3306/test?useUnicode=true&characterEncoding=UTF-8&allowMultiQueries=true</master.jdbc.url>
<master.username>root</master.username>
<master.password>222222</master.password>
</properties>
</profile>
<!-- 预发布环境 -->
<profile>
<id>pre-release</id>
<properties>
<master.driver.className>com.mysql.jdbc.Driver</master.driver.className>
<master.jdbc.url>jdbc:mysql://192.168.91.93:3306/test?useUnicode=true&characterEncoding=UTF-8&allowMultiQueries=true</master.jdbc.url>
<master.username>root</master.username>
<master.password>333333</master.password>
</properties>
</profile>
<!-- 线上环境 -->
<profile>
<id>release</id>
<properties>
<master.driver.className>com.mysql.jdbc.Driver</master.driver.className>
<master.jdbc.url>jdbc:mysql://192.168.91.94:3306/test?useUnicode=true&characterEncoding=UTF-8&allowMultiQueries=true</master.jdbc.url>
<master.username>root</master.username>
<master.password>444444</master.password>
</properties>
</profile>
</profiles>
<build>
<!--环境切换必须配置:如果不设置resource 会导致application.yaml中的@@找不到pom文件中的配置-->
<resources>
<resource>
<directory>src/main/resources</directory>
<filtering>true</filtering>
</resource>
</resources>
<plugins>
<!-- 解决maven install时提示找不到web.xml文件异常 -->
<!-- 异常信息:Failed to execute goal org.apache.maven.plugins:maven-war-plugin:2.2:war (default-war) on project report-web: Error assembling WAR: webxml attribute is required (or pre-existing WEB-INF/web.xml if executing in update mode) -> [Help 1] -->
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-war-plugin</artifactId>
<version>2.4</version>
<configuration>
<failOnMissingWebXml>false</failOnMissingWebXml>
</configuration>
</plugin>
</plugins>
</build>
</project>
<!-- report-service的pom.xml -->
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<parent>
<artifactId>bi-report</artifactId>
<groupId>com.demo</groupId>
<version>1.0-SNAPSHOT</version>
</parent>
<modelVersion>4.0.0</modelVersion>
<artifactId>report-service</artifactId>
<!-- service层依赖utils,本来可以不再service层依赖的,但是基于接口开发,dao层完全用不到utils工具包,pojo也用不到,所以在service层依赖是最合适的。通过Maven依赖传递,web层也会变向依赖utils工具包 -->
<dependencies>
<!--依赖dao、utils-->
<dependency>
<groupId>com.demo</groupId>
<artifactId>report-dao</artifactId>
<version>1.0-SNAPSHOT</version>
</dependency>
<dependency>
<groupId>com.demo</groupId>
<artifactId>report-utils</artifactId>
<version>1.0-SNAPSHOT</version>
</dependency>
</dependencies>
</project>
<!-- report-dao的pom.xml -->
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<parent>
<artifactId>bi-report</artifactId>
<groupId>com.demo</groupId>
<version>1.0-SNAPSHOT</version>
</parent>
<modelVersion>4.0.0</modelVersion>
<artifactId>report-dao</artifactId>
<!-- dao层需要与数据库进行交互,所以在dao层才依赖jdbc、mybatis、druid、mysql驱动,而不是每一个模块都依赖这些,对那些模块也没用的。log4j日志包每个模块会用到(pojo除外),所以在dao层依赖,其他模块通过依赖传递来获取。-->
<dependencies>
<!--dao层依赖pojo-->
<dependency>
<groupId>com.demo</groupId>
<artifactId>report-pojo</artifactId>
<version>1.0-SNAPSHOT</version>
</dependency>
<!--springboot整合jdbc-->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-jdbc</artifactId>
</dependency>
<!--springboot整合mybatis-->
<dependency>
<groupId>org.mybatis.spring.boot</groupId>
<artifactId>mybatis-spring-boot-starter</artifactId>
</dependency>
<!-- springboot整合log4j -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-log4j</artifactId>
</dependency>
<!--springboot整合druid-->
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>druid-spring-boot-starter</artifactId>
</dependency>
<!--mysql驱动-->
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
</dependency>
</dependencies>
<build>
<plugins>
<!-- 该插件也是只在dao层用到,所以在此处引入即可 -->
<!-- mybatis generator 自动生成代码插件 -->
<plugin>
<groupId>org.mybatis.generator</groupId>
<artifactId>mybatis-generator-maven-plugin</artifactId>
<version>1.3.2</version>
<configuration>
<configurationFile>${basedir}/src/main/resources/generator/generatorConfig.xml</configurationFile>
<overwrite>true</overwrite>
<verbose>true</verbose>
</configuration>
</plugin>
</plugins>
</build>
</project>
<!-- report-pojo的pom.xml。什么都没有依赖 -->
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<parent>
<artifactId>bi-report</artifactId>
<groupId>com.demo</groupId>
<version>1.0-SNAPSHOT</version>
</parent>
<modelVersion>4.0.0</modelVersion>
<artifactId>report-pojo</artifactId>
</project>
<!-- report-utils的pom.xml -->
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<parent>
<artifactId>bi-report</artifactId>
<groupId>com.demo</groupId>
<version>1.0-SNAPSHOT</version>
</parent>
<modelVersion>4.0.0</modelVersion>
<artifactId>report-utils</artifactId>
<dependencies>
<!--commons-lang3工具包-->
<dependency>
<groupId>org.apache.commons</groupId>
<artifactId>commons-lang3</artifactId>
</dependency>
</dependencies>
</project>
具体代码实现部分(部分)
package com.report.controller;
import com.report.pojo.User;
import com.report.service.inter.IUserService;
import com.report.utils.StringUtil;
import com.report.vo.Constants;
import com.report.vo.ResultData;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;
import javax.annotation.Resource;
import java.util.List;
/**
* web模块Controller
*/
@Controller
@RequestMapping("user")
public class UserController{
@Resource(name="userService")
private IUserService userService;
/**
* 获取用户信息
*/
@RequestMapping("queryUser.htm")
@ResponseBody
public ResultData<List<User>> queryUser(){
ResultData<List<User>> result = new ResultData<List<User>>();
List<User> data = null;
try{
data = userService.queryUser();
}catch (Exception e){
result.setCode(Constants.DATA_QUERY_EXCEPTION);
result.setMessage(Constants.DATA_QUERY_EXCEPTION_MSG);
}
result.setData(data);
return result;
}
/**
* 获取用户信息[直接返回到页面]
* @param view
* @return
*/
@RequestMapping("getUser.htm")
public ModelAndView getUser(ModelAndView view){
view.setViewName("index");
//调用report-utils中的方法
String nm = StringUtil.firstAlphabetUpperCase("Mr.Liu");
System.out.println(nm);
List<User> data = userService.queryUser();
view.addObject("userList",data);
return view;
}
}
package com.report.service.inter;
import com.report.pojo.User;
import java.util.List;
/**
* 用户Service接口层
*/
public interface IUserService {
/**
* 查询用户信息
*/
List<User> queryUser();
}
package com.report.service.impl;
import com.report.dao.master.UserMapper;
import com.report.pojo.User;
import com.report.pojo.UserExample;
import com.report.service.inter.IUserService;
import org.springframework.stereotype.Service;
import javax.annotation.Resource;
import java.util.List;
/**
* 用户Service实现类
*/
@Service("userService")
public class UserServiceImpl implements IUserService {
@Resource(name = "userDao")
private UserMapper userDao;
/**
* 查询用户信息
*/
public List<User> queryUser() {
UserExample userEx = new UserExample();
userEx.createCriteria().andIdIsNotNull();
return userDao.selectByExample(userEx);
}
}
package com.report.dao.master;
import com.report.pojo.User;
import com.report.pojo.UserExample;
import java.util.List;
import org.apache.ibatis.annotations.Param;
import org.springframework.stereotype.Repository;
/**
* dao层
*/
@Repository("userDao")
public interface UserMapper {
int countByExample(UserExample example);
int deleteByExample(UserExample example);
int deleteByPrimaryKey(Integer id);
int insert(User record);
int insertSelective(User record);
List<User> selectByExample(UserExample example);
User selectByPrimaryKey(Integer id);
int updateByExampleSelective(@Param("record") User record, @Param("example") UserExample example);
int updateByExample(@Param("record") User record, @Param("example") UserExample example);
int updateByPrimaryKeySelective(User record);
int updateByPrimaryKey(User record);
}
<!-- xmlSql部分(generator自动生成的Sql) -->
<?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="com.report.dao.master.UserMapper" >
<resultMap id="BaseResultMap" type="com.report.pojo.User" >
<id column="id" property="id" jdbcType="INTEGER" />
<result column="username" property="username" jdbcType="VARCHAR" />
<result column="age" property="age" jdbcType="INTEGER" />
<result column="address" property="address" jdbcType="VARCHAR" />
</resultMap>
<sql id="Example_Where_Clause" >
<where >
<foreach collection="oredCriteria" item="criteria" separator="or" >
<if test="criteria.valid" >
<trim prefix="(" suffix=")" prefixOverrides="and" >
<foreach collection="criteria.criteria" item="criterion" >
<choose >
<when test="criterion.noValue" >
and ${criterion.condition}
</when>
<when test="criterion.singleValue" >
and ${criterion.condition} #{criterion.value}
</when>
<when test="criterion.betweenValue" >
and ${criterion.condition} #{criterion.value} and #{criterion.secondValue}
</when>
<when test="criterion.listValue" >
and ${criterion.condition}
<foreach collection="criterion.value" item="listItem" open="(" close=")" separator="," >
#{listItem}
</foreach>
</when>
</choose>
</foreach>
</trim>
</if>
</foreach>
</where>
</sql>
<sql id="Update_By_Example_Where_Clause" >
<where >
<foreach collection="example.oredCriteria" item="criteria" separator="or" >
<if test="criteria.valid" >
<trim prefix="(" suffix=")" prefixOverrides="and" >
<foreach collection="criteria.criteria" item="criterion" >
<choose >
<when test="criterion.noValue" >
and ${criterion.condition}
</when>
<when test="criterion.singleValue" >
and ${criterion.condition} #{criterion.value}
</when>
<when test="criterion.betweenValue" >
and ${criterion.condition} #{criterion.value} and #{criterion.secondValue}
</when>
<when test="criterion.listValue" >
and ${criterion.condition}
<foreach collection="criterion.value" item="listItem" open="(" close=")" separator="," >
#{listItem}
</foreach>
</when>
</choose>
</foreach>
</trim>
</if>
</foreach>
</where>
</sql>
<sql id="Base_Column_List" >
id, username, age, address
</sql>
<select id="selectByExample" resultMap="BaseResultMap" parameterType="com.report.pojo.UserExample" >
select
<if test="distinct" >
distinct
</if>
'true' as QUERYID,
<include refid="Base_Column_List" />
from user
<if test="_parameter != null" >
<include refid="Example_Where_Clause" />
</if>
<if test="orderByClause != null" >
order by ${orderByClause}
</if>
</select>
<select id="selectByPrimaryKey" resultMap="BaseResultMap" parameterType="java.lang.Integer" >
select
<include refid="Base_Column_List" />
from user
where id = #{id,jdbcType=INTEGER}
</select>
<delete id="deleteByPrimaryKey" parameterType="java.lang.Integer" >
delete from user
where id = #{id,jdbcType=INTEGER}
</delete>
<delete id="deleteByExample" parameterType="com.report.pojo.UserExample" >
delete from user
<if test="_parameter != null" >
<include refid="Example_Where_Clause" />
</if>
</delete>
<insert id="insert" parameterType="com.report.pojo.User" >
insert into user (id, username, age,
address)
values (#{id,jdbcType=INTEGER}, #{username,jdbcType=VARCHAR}, #{age,jdbcType=INTEGER},
#{address,jdbcType=VARCHAR})
</insert>
<insert id="insertSelective" parameterType="com.report.pojo.User" >
insert into user
<trim prefix="(" suffix=")" suffixOverrides="," >
<if test="id != null" >
id,
</if>
<if test="username != null" >
username,
</if>
<if test="age != null" >
age,
</if>
<if test="address != null" >
address,
</if>
</trim>
<trim prefix="values (" suffix=")" suffixOverrides="," >
<if test="id != null" >
#{id,jdbcType=INTEGER},
</if>
<if test="username != null" >
#{username,jdbcType=VARCHAR},
</if>
<if test="age != null" >
#{age,jdbcType=INTEGER},
</if>
<if test="address != null" >
#{address,jdbcType=VARCHAR},
</if>
</trim>
</insert>
<select id="countByExample" parameterType="com.report.pojo.UserExample" resultType="java.lang.Integer" >
select count(*) from user
<if test="_parameter != null" >
<include refid="Example_Where_Clause" />
</if>
</select>
<update id="updateByExampleSelective" parameterType="map" >
update user
<set >
<if test="record.id != null" >
id = #{record.id,jdbcType=INTEGER},
</if>
<if test="record.username != null" >
username = #{record.username,jdbcType=VARCHAR},
</if>
<if test="record.age != null" >
age = #{record.age,jdbcType=INTEGER},
</if>
<if test="record.address != null" >
address = #{record.address,jdbcType=VARCHAR},
</if>
</set>
<if test="_parameter != null" >
<include refid="Update_By_Example_Where_Clause" />
</if>
</update>
<update id="updateByExample" parameterType="map" >
update user
set id = #{record.id,jdbcType=INTEGER},
username = #{record.username,jdbcType=VARCHAR},
age = #{record.age,jdbcType=INTEGER},
address = #{record.address,jdbcType=VARCHAR}
<if test="_parameter != null" >
<include refid="Update_By_Example_Where_Clause" />
</if>
</update>
<update id="updateByPrimaryKeySelective" parameterType="com.report.pojo.User" >
update user
<set >
<if test="username != null" >
username = #{username,jdbcType=VARCHAR},
</if>
<if test="age != null" >
age = #{age,jdbcType=INTEGER},
</if>
<if test="address != null" >
address = #{address,jdbcType=VARCHAR},
</if>
</set>
where id = #{id,jdbcType=INTEGER}
</update>
<update id="updateByPrimaryKey" parameterType="com.report.pojo.User" >
update user
set username = #{username,jdbcType=VARCHAR},
age = #{age,jdbcType=INTEGER},
address = #{address,jdbcType=VARCHAR}
where id = #{id,jdbcType=INTEGER}
</update>
</mapper>
<!-- View层:Index.html -->
<!DOCTYPE html>
<html xmlns:th="http://www.thymeleaf.org">
<head>
<meta charset="UTF-8">
<title>Index页面</title>
<link href="../static/plugins/bootstrap/css/bootstrap.min.css" rel="stylesheet">
<link href="../static/plugins/dataTables/css/dataTables.bootstrap.css" rel="stylesheet">
<link href="../static/css/style.min.css" rel="stylesheet">
<script src="../static/js/jquery/jquery.min.js"></script>
<script src="../static/plugins/bootstrap/js/bootstrap.min.js"></script>
<script src="../static/plugins/dataTables/js/jquery.dataTables.js"></script>
<script src="../static/plugins/dataTables/js/dataTables.bootstrap.js"></script>
</head>
<body>
<!-- 循环遍历 -->
<div class="wrapper wrapper-content">
<table class="table table-striped table-bordered table-hover dataTables-example">
<thead>
<tr>
<th>id</th>
<th>name</th>
<th>age</th>
<th>address</th>
</tr>
</thead>
<tbody>
<tr th:each="user:${userList}">
<td th:text="${user.id}"></td>
<td th:text="${user.username}"></td>
<td th:text="${user.age}"></td>
<td th:text="${user.address}"></td>
</tr>
</tbody>
</table>
</div>
</body>
<script>
$(document).ready(function () {
$("table").dataTable();
});
</script>
</html>
实现效果
①接口开发
②表现层展示
如有需要Demo实例,有不懂的地方等,可联系我。demo仅作参考使用
如有错误的地方,也请您能够帮我指出,我们一起学习,一起进步,谢谢大家。