一、环境:
IDEA+SpringBoot+MySql+MyBatis+Thymeleaf
GitHub源码地址:
https://github.com/516516/springboottestdemo
二、搭建好的项目总览:![](https://i-blog.csdnimg.cn/blog_migrate/5b03dcec1f5afadae20c0a83ee33d76c.png)
访问测试:
三、项目搭建:
1.File --> new --> project :
initializr: https://start.aliyun.com【初始化选择 使用 阿里云】【速度快--亲测】
next
完善上述信息 next
依赖选择:Spring Web + Thymeleaf+ JDBC API \MySql Driver\MyBatis Framework
next
选择完项目地址后,finish
2.此时项目目录结构
3.Maven 仓库地址配置
4.试运行项目 按目录下述目录结构创建相应文件
4.1创建controller
@Controller
public class HelloController {
@RequestMapping("index")
public String sayHello(){
return "index";
}
}
4.2创建index.html页面
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Title</title>
</head>
<body>
<h1>Hello World!!!!!</h1>
</body>
</html>
4.3配置文件改后缀 yml,结构清晰,便于后续配置。
spring:
datasource:
name: study
url: jdbc:mysql://localhost:3306/study?useUnicode=true&characterEncoding=UTF-8&autoReconnect=true&autoReconnectForPools=true&allowMultiQueries=true&zeroDateTimeBehavior=convertToNull&serverTimezone=UTC
username: root
password: 516516
driver-class-name: com.mysql.cj.jdbc.Driver
4.4 maven clean install
4.5运行
4.6测试:
5.整合 MyBatis: 按一下目录结构 创建相关文件夹及文件 文件目录不解释了,不知道什么意思的建议先玩玩spring。
ModelDes
package com.example.demo.bean;
public class ModelDes {
private Long id;
private Long modelId;
private String description;
private String updateUser;
private String createUser;
private Byte yn;
public Long getId() {
return id;
}
public void setId(Long id) {
this.id = id;
}
public Long getModelId() {
return modelId;
}
public void setModelId(Long modelId) {
this.modelId = modelId;
}
public String getDescription() {
return description;
}
public void setDescription(String description) {
this.description = description == null ? null : description.trim();
}
public String getUpdateUser() {
return updateUser;
}
public void setUpdateUser(String updateUser) {
this.updateUser = updateUser == null ? null : updateUser.trim();
}
public String getCreateUser() {
return createUser;
}
public void setCreateUser(String createUser) {
this.createUser = createUser == null ? null : createUser.trim();
}
public Byte getYn() {
return yn;
}
public void setYn(Byte yn) {
this.yn = yn;
}
}
HelloController
package com.example.demo.controller;
import com.example.demo.bean.ModelDes;
import com.example.demo.service.TestService;
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.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import java.util.HashMap;
@Controller
public class HelloController {
@Autowired
TestService service;
@RequestMapping("index")
public String sayHello(){
return "index";
}
@ResponseBody
@RequestMapping(value="query",method = RequestMethod.POST)
public HashMap <String,Object> queryModelDes(){
ModelDes des= service.queryById(1L);
HashMap<String,Object> result=new HashMap <>();
result.put("dex",des);
return result;
}
}
ModelDesMapper
package com.example.demo.mapper;
import com.example.demo.bean.ModelDes;
public interface ModelDesMapper {
int deleteByPrimaryKey(Long id);
int insert(ModelDes record);
int insertSelective(ModelDes record);
ModelDes selectByPrimaryKey(Long id);
int updateByPrimaryKeySelective(ModelDes record);
int updateByPrimaryKey(ModelDes record);
}
TestService
package com.example.demo.service;
import com.example.demo.bean.ModelDes;
public interface TestService {
ModelDes queryById(Long id);
}
TestServiceImpl
package com.example.demo.service.impl;
import com.example.demo.bean.ModelDes;
import com.example.demo.mapper.ModelDesMapper;
import com.example.demo.service.TestService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
@Service
public class TestServiceImpl implements TestService {
@Autowired
private ModelDesMapper mapper;
@Override
public ModelDes queryById(Long id) {
return mapper.selectByPrimaryKey(id);
}
}
DemoApplication
package com.example.demo;
import org.mybatis.spring.annotation.MapperScan;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
@SpringBootApplication
@MapperScan("com.example.demo.mapper")
public class DemoApplication {
public static void main(String[] args) {
SpringApplication.run(DemoApplication.class, args);
}
}
ModelDesMapper.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.example.demo.mapper.ModelDesMapper" >
<resultMap id="BaseResultMap" type="com.example.demo.bean.ModelDes" >
<id column="id" property="id" jdbcType="BIGINT" />
<result column="model_id" property="modelId" jdbcType="BIGINT" />
<result column="description" property="description" jdbcType="VARCHAR" />
<result column="update_user" property="updateUser" jdbcType="VARCHAR" />
<result column="create_user" property="createUser" jdbcType="VARCHAR" />
<result column="yn" property="yn" jdbcType="TINYINT" />
</resultMap>
<sql id="Base_Column_List" >
id, model_id, description, update_user, create_user, yn
</sql>
<select id="selectByPrimaryKey" resultMap="BaseResultMap" parameterType="java.lang.Long" >
select
<include refid="Base_Column_List" />
from model_des
where id = #{id,jdbcType=BIGINT}
</select>
<delete id="deleteByPrimaryKey" parameterType="java.lang.Long" >
delete from model_des
where id = #{id,jdbcType=BIGINT}
</delete>
<insert id="insert" parameterType="com.example.demo.bean.ModelDes" >
insert into model_des (id, model_id, description,
update_user, create_user, yn
)
values (#{id,jdbcType=BIGINT}, #{modelId,jdbcType=BIGINT}, #{description,jdbcType=VARCHAR},
#{updateUser,jdbcType=VARCHAR}, #{createUser,jdbcType=VARCHAR}, #{yn,jdbcType=TINYINT}
)
</insert>
<insert id="insertSelective" parameterType="com.example.demo.bean.ModelDes" >
insert into model_des
<trim prefix="(" suffix=")" suffixOverrides="," >
<if test="id != null" >
id,
</if>
<if test="modelId != null" >
model_id,
</if>
<if test="description != null" >
description,
</if>
<if test="updateUser != null" >
update_user,
</if>
<if test="createUser != null" >
create_user,
</if>
<if test="yn != null" >
yn,
</if>
</trim>
<trim prefix="values (" suffix=")" suffixOverrides="," >
<if test="id != null" >
#{id,jdbcType=BIGINT},
</if>
<if test="modelId != null" >
#{modelId,jdbcType=BIGINT},
</if>
<if test="description != null" >
#{description,jdbcType=VARCHAR},
</if>
<if test="updateUser != null" >
#{updateUser,jdbcType=VARCHAR},
</if>
<if test="createUser != null" >
#{createUser,jdbcType=VARCHAR},
</if>
<if test="yn != null" >
#{yn,jdbcType=TINYINT},
</if>
</trim>
</insert>
<update id="updateByPrimaryKeySelective" parameterType="com.example.demo.bean.ModelDes" >
update model_des
<set >
<if test="modelId != null" >
model_id = #{modelId,jdbcType=BIGINT},
</if>
<if test="description != null" >
description = #{description,jdbcType=VARCHAR},
</if>
<if test="updateUser != null" >
update_user = #{updateUser,jdbcType=VARCHAR},
</if>
<if test="createUser != null" >
create_user = #{createUser,jdbcType=VARCHAR},
</if>
<if test="yn != null" >
yn = #{yn,jdbcType=TINYINT},
</if>
</set>
where id = #{id,jdbcType=BIGINT}
</update>
<update id="updateByPrimaryKey" parameterType="com.example.demo.bean.ModelDes" >
update model_des
set model_id = #{modelId,jdbcType=BIGINT},
description = #{description,jdbcType=VARCHAR},
update_user = #{updateUser,jdbcType=VARCHAR},
create_user = #{createUser,jdbcType=VARCHAR},
yn = #{yn,jdbcType=TINYINT}
where id = #{id,jdbcType=BIGINT}
</update>
</mapper>
application.yml文件
spring:
datasource:
name: study
url: jdbc:mysql://localhost:3306/study?useUnicode=true&characterEncoding=UTF-8&autoReconnect=true&autoReconnectForPools=true&allowMultiQueries=true&zeroDateTimeBehavior=convertToNull&serverTimezone=UTC
username: root
password: 516516
driver-class-name: com.mysql.cj.jdbc.Driver
mybatis:
mapper-locations: classpath:mapper/*.xml
type-aliases-package: com.knowledge.system.springbootdemo.bean
注意yml文件配置中mapper-locations的坑(多级目录):
mapper-locations: classpath:com/springcloud/personal/system/springclouddemomvcservice/mapper/*.xml
启动项目:
访问测试: