【配置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>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.1.3.RELEASE</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<groupId>com.cn.demo</groupId>
<artifactId>springboot-demo</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>springboot-demo</name>
<description>Demo project for Spring Boot</description>
<properties>
<!-- jdk -->
<java.version>1.8</java.version>
<!-- 项目编码-->
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
</properties>
<dependencies>
<!-- springboot -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter</artifactId>
</dependency>
<!-- mybatis -->
<dependency>
<groupId>org.mybatis.spring.boot</groupId>
<artifactId>mybatis-spring-boot-starter</artifactId>
<version>2.0.0</version>
</dependency>
<!-- 数据库 -->
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>5.1.6</version>
</dependency>
<!-- 连接池 -->
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>druid</artifactId>
<version>1.1.0</version>
</dependency>
<!-- mybatis插件 -->
<dependency>
<groupId>org.mybatis.generator</groupId>
<artifactId>mybatis-generator-core</artifactId>
<version>1.3.7</version>
</dependency>
<!-- 如不 引用@Param注解无法使用 -->
<dependency>
<groupId>org.apache.ibatis</groupId>
<artifactId>ibatis-core</artifactId>
<version>3.0</version>
</dependency>
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>javax.servlet-api</artifactId>
</dependency>
</dependencies>
</project>
【配置application.yml文件】
注:将application.properties改后缀名为.yml
server:
port: 80 #端口号
tomcat:
uri-encoding: UTF-8 #编码格式
spring:
datasource:
url: jdbc:mysql://localhost:3306/demo?characterEncoding=UTF-8 #数据库地址
username: root #用户名
password: root #密码
driver-class-name: com.mysql.jdbc.Driver #数据库驱动
type: com.alibaba.druid.pool.DruidDataSource #连接池
mybatis:
mapper-locations: classpath:mapper/*Mapper.xml #mapper映射xml文件的所在路径(一般都在resources目录下,*代表任意字符)
type-aliases-package: com.cn.model # 实体类存放路径
【使用Mybatis逆向工程生成Dao与 映射文件】
参考【MyBatis】MyBatis逆向工程mybatis-generator的使用(MySql)
【controller】
@RestController:用在类上 ,声明充当控制层
@RequestMapping:用在类上,表示为 命名空间;用在方法上,表示为访问的方法名;
@Resource:按属性注入Bean的方式
package com.cn.demo.user.controller;
import java.util.List;
import javax.annotation.Resource;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.cn.demo.model.User;
import com.cn.demo.user.service.UserService;
@RestController
@RequestMapping("/user")
public class UserController {
@Resource
private UserService userService;
@RequestMapping("/queryUserList")
public List<User> queryUserList() {
List<User> userList = userService.queryUserList();
return userList;
}
}
【service】
package com.cn.demo.user.service;
import java.util.List;
import com.cn.demo.model.User;
public interface UserService {
List<User> queryUserList();
}
【Service Impl】
@Service():里面的值必须与Controller中保持一致
package com.cn.demo.user.service.impl;
import java.util.List;
import javax.annotation.Resource;
import org.springframework.stereotype.Service;
import com.cn.demo.model.User;
import com.cn.demo.user.dao.UserMapper;
import com.cn.demo.user.service.UserService;
@Service("userService")
public class UserServiceImpl implements UserService {
@Resource
private UserMapper userMapper;
@Override
public List<User> queryUserList() {
// TODO Auto-generated method stub
List<User> userList = userMapper.selectByExample(null);
return userList;
}
}
【dao】
package com.cn.demo.user.dao;
import com.cn.demo.model.User;
import com.cn.demo.model.UserExample;
import java.util.List;
import org.apache.ibatis.annotations.Param;
public interface UserMapper {
int countByExample(UserExample example);
int deleteByExample(UserExample example);
int insert(User record);
int insertSelective(User record);
List<User> selectByExample(UserExample example);
int updateByExampleSelective(@Param("record") User record, @Param("example") UserExample example);
int updateByExample(@Param("record") User record, @Param("example") UserExample example);
}
【mapper.xml】
<?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.cn.demo.user.dao.UserMapper" >
<resultMap id="BaseResultMap" type="com.cn.demo.model.User" >
<result column="user_id" property="userId" jdbcType="VARCHAR" />
<result column="user_name" property="userName" jdbcType="VARCHAR" />
<result column="user_sex" property="userSex" jdbcType="INTEGER" />
</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" >
user_id, user_name, user_sex
</sql>
<select id="selectByExample" resultMap="BaseResultMap" parameterType="com.cn.demo.model.UserExample" >
select
<if test="distinct" >
distinct
</if>
'true' as QUERYID,
<include refid="Base_Column_List" />
from t_user
<if test="_parameter != null" >
<include refid="Example_Where_Clause" />
</if>
<if test="orderByClause != null" >
order by ${orderByClause}
</if>
</select>
<delete id="deleteByExample" parameterType="com.cn.demo.model.UserExample" >
delete from t_user
<if test="_parameter != null" >
<include refid="Example_Where_Clause" />
</if>
</delete>
<insert id="insert" parameterType="com.cn.demo.model.User" >
insert into t_user (user_id, user_name, user_sex
)
values (#{userId,jdbcType=VARCHAR}, #{userName,jdbcType=VARCHAR}, #{userSex,jdbcType=INTEGER}
)
</insert>
<insert id="insertSelective" parameterType="com.cn.demo.model.User" >
insert into t_user
<trim prefix="(" suffix=")" suffixOverrides="," >
<if test="userId != null" >
user_id,
</if>
<if test="userName != null" >
user_name,
</if>
<if test="userSex != null" >
user_sex,
</if>
</trim>
<trim prefix="values (" suffix=")" suffixOverrides="," >
<if test="userId != null" >
#{userId,jdbcType=VARCHAR},
</if>
<if test="userName != null" >
#{userName,jdbcType=VARCHAR},
</if>
<if test="userSex != null" >
#{userSex,jdbcType=INTEGER},
</if>
</trim>
</insert>
<select id="countByExample" parameterType="com.cn.demo.model.UserExample" resultType="java.lang.Integer" >
select count(*) from t_user
<if test="_parameter != null" >
<include refid="Example_Where_Clause" />
</if>
</select>
<update id="updateByExampleSelective" parameterType="map" >
update t_user
<set >
<if test="record.userId != null" >
user_id = #{record.userId,jdbcType=VARCHAR},
</if>
<if test="record.userName != null" >
user_name = #{record.userName,jdbcType=VARCHAR},
</if>
<if test="record.userSex != null" >
user_sex = #{record.userSex,jdbcType=INTEGER},
</if>
</set>
<if test="_parameter != null" >
<include refid="Update_By_Example_Where_Clause" />
</if>
</update>
<update id="updateByExample" parameterType="map" >
update t_user
set user_id = #{record.userId,jdbcType=VARCHAR},
user_name = #{record.userName,jdbcType=VARCHAR},
user_sex = #{record.userSex,jdbcType=INTEGER}
<if test="_parameter != null" >
<include refid="Update_By_Example_Where_Clause" />
</if>
</update>
</mapper>
【启动类:SpringbootDemoApplication】
@SpringBootApplication:声明是一个启动类
@MapperScan():动态是扫描映射文件
package com.cn.demo;
import org.mybatis.spring.annotation.MapperScan;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
@SpringBootApplication
@MapperScan("com.cn.demo.*.dao")
public class SpringbootDemoApplication {
public static void main(String[] args) {
SpringApplication.run(SpringbootDemoApplication.class, args);
}
}
【访问项目】
通过main方法启动SpringbootDemoApplication类
在浏览器地址输入http://localhost/user/queryUserList就可以访问了。