SpringBoot之单体应用
1.选择springInitializer
2.填写包名项目名
3.选择如下框架
4.项目结构如下
5.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 https://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.2.6.RELEASE</version> <relativePath/> <!-- lookup parent from repository --> </parent> <groupId>com.blb</groupId> <artifactId>ysearch</artifactId> <version>0.0.1-SNAPSHOT</version> <name>ysearch</name> <description>Demo project for Spring Boot</description> <properties> <java.version>1.8</java.version> </properties> <dependencies> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> </dependency> <dependency> <groupId>org.mybatis.spring.boot</groupId> <artifactId>mybatis-spring-boot-starter</artifactId> <version>2.1.2</version> </dependency> <dependency> <groupId>com.alibaba</groupId> <artifactId>druid</artifactId> <version>1.1.10</version> </dependency> <dependency> <groupId>mysql</groupId> <artifactId>mysql-connector-java</artifactId> <scope>runtime</scope> </dependency> <dependency> <groupId>org.projectlombok</groupId> <artifactId>lombok</artifactId> <optional>true</optional> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-test</artifactId> <scope>test</scope> <exclusions> <exclusion> <groupId>org.junit.vintage</groupId> <artifactId>junit-vintage-engine</artifactId> </exclusion> </exclusions> </dependency> </dependencies> <build> <resources> <resource> <directory>src/main/java</directory> <includes> <include>**/*.xml</include> </includes> </resource> </resources> <plugins> <plugin> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-maven-plugin</artifactId> </plugin> </plugins> </build> </project>
6.pojo包下TUser类
package com.blb.ysearch.pojo; public class TUser { private Integer uid; private String uname; private String upwd; public Integer getUid() { return uid; } public void setUid(Integer uid) { this.uid = uid; } public String getUname() { return uname; } public void setUname(String uname) { this.uname = uname; } public String getUpwd() { return upwd; } public void setUpwd(String upwd) { this.upwd = upwd; } }
7.dao层
TUserMapper类:
package com.blb.ysearch.dao; import com.blb.ysearch.pojo.TUser; import org.apache.ibatis.annotations.Mapper; @Mapper public interface TUserMapper { int deleteByPrimaryKey(Integer uid); int insert(TUser record); int insertSelective(TUser record); TUser selectByPrimaryKey(Integer uid); int updateByPrimaryKeySelective(TUser record); int updateByPrimaryKey(TUser record); }
TUserMapper.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.blb.ysearch.dao.TUserMapper"> <resultMap id="BaseResultMap" type="com.blb.ysearch.pojo.TUser"> <!--@mbg.generated--> <!--@Table t_user--> <id column="uid" jdbcType="INTEGER" property="uid" /> <result column="uname" jdbcType="VARCHAR" property="uname" /> <result column="upwd" jdbcType="VARCHAR" property="upwd" /> </resultMap> <sql id="Base_Column_List"> <!--@mbg.generated--> `uid`, uname, upwd </sql> <select id="selectByPrimaryKey" parameterType="java.lang.Integer" resultMap="BaseResultMap"> <!--@mbg.generated--> select <include refid="Base_Column_List" /> from t_user where `uid` = #{uid,jdbcType=INTEGER} </select> <delete id="deleteByPrimaryKey" parameterType="java.lang.Integer"> <!--@mbg.generated--> delete from t_user where `uid` = #{uid,jdbcType=INTEGER} </delete> <insert id="insert" parameterType="com.blb.ysearch.pojo.TUser"> <!--@mbg.generated--> insert into t_user (`uid`, uname, upwd ) values (#{uid,jdbcType=INTEGER}, #{uname,jdbcType=VARCHAR}, #{upwd,jdbcType=VARCHAR} ) </insert> <insert id="insertSelective" parameterType="com.blb.ysearch.pojo.TUser"> <!--@mbg.generated--> insert into t_user <trim prefix="(" suffix=")" suffixOverrides=","> <if test="uid != null"> `uid`, </if> <if test="uname != null"> uname, </if> <if test="upwd != null"> upwd, </if> </trim> <trim prefix="values (" suffix=")" suffixOverrides=","> <if test="uid != null"> #{uid,jdbcType=INTEGER}, </if> <if test="uname != null"> #{uname,jdbcType=VARCHAR}, </if> <if test="upwd != null"> #{upwd,jdbcType=VARCHAR}, </if> </trim> </insert> <update id="updateByPrimaryKeySelective" parameterType="com.blb.ysearch.pojo.TUser"> <!--@mbg.generated--> update t_user <set> <if test="uname != null"> uname = #{uname,jdbcType=VARCHAR}, </if> <if test="upwd != null"> upwd = #{upwd,jdbcType=VARCHAR}, </if> </set> where `uid` = #{uid,jdbcType=INTEGER} </update> <update id="updateByPrimaryKey" parameterType="com.blb.ysearch.pojo.TUser"> <!--@mbg.generated--> update t_user set uname = #{uname,jdbcType=VARCHAR}, upwd = #{upwd,jdbcType=VARCHAR} where `uid` = #{uid,jdbcType=INTEGER} </update> </mapper>
8.service层
TUserService接口类:
package com.blb.ysearch.service; import com.blb.ysearch.pojo.TUser; public interface TUserService { public void save(TUser user); public void remove(Integer uid); public void update(TUser user); public TUser get(Integer uid); }
Impl包下TUserService实现类:
package com.blb.ysearch.service.impl; import com.blb.ysearch.dao.TUserMapper; import com.blb.ysearch.pojo.TUser; import com.blb.ysearch.service.TUserService; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Service; import org.springframework.transaction.annotation.Transactional; @Service @Transactional public class TUserServiceImpl implements TUserService { @Autowired private TUserMapper tUserMapper; @Override public void save(TUser user) { tUserMapper.insert(user); } @Override public void remove(Integer uid) { tUserMapper.deleteByPrimaryKey(uid); } @Override public void update(TUser user) { tUserMapper.updateByPrimaryKey(user); } @Transactional(readOnly = true) public TUser get(Integer uid) { TUser tUser = tUserMapper.selectByPrimaryKey(uid); return tUser; } }
9.controller层
HelloController类:
package com.blb.ysearch.controller; import com.blb.ysearch.pojo.TUser; import com.blb.ysearch.service.TUserService; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.web.bind.annotation.*; import java.util.HashMap; import java.util.Map; @RestController public class HelloController { @Autowired private TUserService tUserService; @RequestMapping(value = "/user/add",method = RequestMethod.POST) public Map add(@RequestBody TUser user){ tUserService.save(user); Map map=new HashMap(); map.put("msg","保存成功"); map.put("status",true); return map; } @RequestMapping(value = "/user/{id}",method = RequestMethod.GET) public Map get(@PathVariable("id")Integer id){ TUser user = tUserService.get(id); Map map=new HashMap(); map.put("msg","查询成功"); map.put("status",true); map.put("data",user); return map; } }
10.根包下YsearchApplication类
package com.blb.ysearch; import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; @SpringBootApplication public class YsearchApplication { public static void main(String[] args) { SpringApplication.run(YsearchApplication.class, args); } }
11.resources文件夹内application.yml
server: port: 9091 spring: mybatis: mapperLocations:classpath:com/blb/ysearch/dao/*.xml typeAliasesPackage:com.blb.ysearch.pojo datasource: name: mysql type: com.alibaba.druid.pool.DruidDataSource driver-class-name: com.mysql.jdbc.Driver url: jdbc:mysql://127.0.0.1:6666/ssm?useUnicode=true&characterEncoding=utf8&useSSL=false&serverTimezone=UTC username: root password: 123456 druid: initial-size: 5 min-idle: 5 max-active: 20 max-wait: 30000 time-between-eviction-runs-millis: 60000 min-evictable-idle-time-millis: 300000 validation-query: select 1 test-while-idle: true test-on-borrow: false test-on-return: false pool-prepared-statements: false max-pool-prepared-statement-per-connection-size: 20 connectionProperties: druid.stat.mergeSql=true;druid.stat.slowSqlMillis=6000