SpringBoot之单体应用

2021-02-13 22:20

阅读:553

标签:setuid   private   nts   arc   prim   primary   文件夹   ide   ado   

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.0modelVersion>
    parent>
        groupId>org.springframework.bootgroupId>
        artifactId>spring-boot-starter-parentartifactId>
        version>2.2.6.RELEASEversion>
        relativePath/> 
    parent>
    groupId>com.blbgroupId>
    artifactId>ysearchartifactId>
    version>0.0.1-SNAPSHOTversion>
    name>ysearchname>
    description>Demo project for Spring Bootdescription>

    properties>
        java.version>1.8java.version>
    properties>

    dependencies>
        dependency>
            groupId>org.springframework.bootgroupId>
            artifactId>spring-boot-starter-webartifactId>
        dependency>
        dependency>
            groupId>org.mybatis.spring.bootgroupId>
            artifactId>mybatis-spring-boot-starterartifactId>
            version>2.1.2version>
        dependency>
        dependency>
            groupId>com.alibabagroupId>
            artifactId>druidartifactId>
            version>1.1.10version>
        dependency>
        dependency>
            groupId>mysqlgroupId>
            artifactId>mysql-connector-javaartifactId>
            scope>runtimescope>
        dependency>
        dependency>
            groupId>org.projectlombokgroupId>
            artifactId>lombokartifactId>
            optional>trueoptional>
        dependency>
        dependency>
            groupId>org.springframework.bootgroupId>
            artifactId>spring-boot-starter-testartifactId>
            scope>testscope>
            exclusions>
                exclusion>
                    groupId>org.junit.vintagegroupId>
                    artifactId>junit-vintage-engineartifactId>
                exclusion>
            exclusions>
        dependency>
    dependencies>

    build>
        resources>
            resource>
                directory>src/main/javadirectory>
                includes>
                    include>**/*.xmlinclude>
                includes>
            resource>
        resources>
        plugins>
            plugin>
                groupId>org.springframework.bootgroupId>
                artifactId>spring-boot-maven-pluginartifactId>
            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">
    
    
    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">
    
    `uid`, uname, upwd
  sql>
  select id="selectByPrimaryKey" parameterType="java.lang.Integer" resultMap="BaseResultMap">
    
    select 
    include refid="Base_Column_List" />
    from t_user
    where `uid` = #{uid,jdbcType=INTEGER}
  select>
  delete id="deleteByPrimaryKey" parameterType="java.lang.Integer">
    
    delete from t_user
    where `uid` = #{uid,jdbcType=INTEGER}
  delete>
  insert id="insert" parameterType="com.blb.ysearch.pojo.TUser">
    
    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">
    
    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">
    
    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">
    
    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

 

SpringBoot之单体应用

标签:setuid   private   nts   arc   prim   primary   文件夹   ide   ado   

原文地址:https://www.cnblogs.com/asksk/p/12725501.html


评论


亲,登录后才可以留言!