STS+Spring boot+MyBatis和 SQL Server实践 学习Java 写下测试用例

此项目借鉴https://blog.csdn.net/wangshuaiwsws95/article/details/85059207此贴完成,为了看到此贴的人能顺序了解,减少不必要的麻烦。

项目目录结构如下

 

下面就是各个截图与代码:STS工具

菜单 file --new--spring starer project    为 “testProject”  我已经建立 系统自己  testProject-1 ,新手更改下

 

然后点击  finish  完成即可。

然后根据一图 建立各个package 

配置 application.properties

server.port=89

# mybatis 配置
mybatis.type-aliases-package=com.testProject.springboot.entity
mybatis.mapper-locations=classpath:Mapper/*.xml
mybatis.configuration.map-underscore-to-camel-case=true

## -------------------------------------------------

## SqlServer 配置
spring.datasource.url=jdbc:sqlserver://HFP-20170511NJC:1433;databasename=AirportData
spring.datasource.driver-class-name=com.microsoft.sqlserver.jdbc.SQLServerDriver
spring.datasource.username=sa
spring.datasource.password=modern

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.4.RELEASE</version>
        <relativePath/> <!-- lookup parent from repository -->
        
    </parent>
    <groupId>com.test</groupId>
    <artifactId>testProject</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <name>testProject</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>
        <!--for mybatis -->
        <dependency>
            <groupId>org.mybatis.spring.boot</groupId>
            <artifactId>mybatis-spring-boot-starter</artifactId>
            <version>1.3.2</version>
        </dependency>

        <!--for SqlServer -->

        <dependency>
            <groupId>com.microsoft.sqlserver</groupId>
            <artifactId>mssql-jdbc</artifactId>
            <scope>runtime</scope>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
        </dependency>


        <!-- mybatis分页插件pageHelper -->

        <dependency>

            <groupId>com.github.pagehelper</groupId>

            <artifactId>pagehelper</artifactId>

            <version>5.0.0</version>

        </dependency>

        <dependency>

            <groupId>com.github.pagehelper</groupId>

            <artifactId>pagehelper-spring-boot-autoconfigure</artifactId>

            <version>1.2.3</version>

        </dependency>

        <dependency>

            <groupId>com.github.pagehelper</groupId>

            <artifactId>pagehelper-spring-boot-starter</artifactId>

            <version>1.2.3</version>

        </dependency>

 


    </dependencies>

    <build>
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
            </plugin>
        </plugins>
    </build>

</project>
 

配置

1:TestProjectApplication.java

package com.testProject.springboot;

import org.mybatis.spring.annotation.MapperScan;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;

@SpringBootApplication
@MapperScan({"com.testProject.*.Mapper"})  
public class TestProjectApplication {

    public static void main(String[] args) {
        SpringApplication.run(TestProjectApplication.class, args);
    }

 }


2:UserController.java

package com.testProject.springboot.controller;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;

import com.testProject.springboot.Service.IUserService;
import com.testProject.springboot.entity.User;

@RestController
public class UserController {

    @Autowired
    private IUserService userService;

    @RequestMapping(value = "/getAllUser", method = RequestMethod.GET)
    public List<User> getAllUser() {
        return userService.getAllUsers();
    }

    @RequestMapping(value = "/addUser", method = RequestMethod.POST)
    @ResponseBody

  //  @RequestBody
    public int addUser(User user ) {
        return userService.addUser( user );
    }

    @RequestMapping(value = "/deleteUser", method = RequestMethod.POST)
    @ResponseBody
    public int deleteUser(User user ) {
        return userService.deleteUser( user );
    }

}

3:User.java

package com.testProject.springboot.entity;


public class User {

    private Long userId;
    private String userName;
    private Boolean sex;
    private String createdTime;

    public Long getUserId() {
        return userId;
    }

    public void setUserId(Long userId) {
        this.userId = userId;
    }

    public String getUserName() {
        return userName;
    }

    public void setUserName(String userName) {
        this.userName = userName;
    }

    public Boolean getSex() {
        return sex;
    }

    public void setSex(Boolean sex) {
        this.sex = sex;
    }

    public String getCreatedTime() {
        return createdTime;
    }

    public void setCreatedTime(String createdTime) {
        this.createdTime = createdTime;
    }
}


4:UserMapper.java

package com.testProject.springboot.Mapper;

import java.util.List;

import org.springframework.stereotype.Repository;

import com.testProject.springboot.entity.User;

//public class UserMapper {
//
//}

public interface UserMapper {
    List<User> getAllUsers();
    int addUser( User user );
    int deleteUser( User user );
}


5:UserMapper.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.testProject.springboot.Mapper.UserMapper">

    <resultMap id="userMap" type="com.testProject.springboot.entity.User">
        <id property="userId" column="user_id" javaType="java.lang.Long"></id>
        <result property="userName" column="user_name" javaType="java.lang.String"></result>
        <result property="sex" column="sex" javaType="java.lang.Boolean"></result>
        <result property="createdTime" column="created_time" javaType="java.lang.String"></result>
    </resultMap>

    <select id="getAllUsers" resultMap="userMap">
        select * from user_test
    </select>

    <insert id="addUser" parameterType="com.testProject.springboot.entity.User">
        insert into user_test ( user_id, user_name, sex, created_time ) values ( #{userId}, #{userName}, #{sex}, #{createdTime} )
    </insert>

    <delete id="deleteUser" parameterType="com.testProject.springboot.entity.User">
        delete from user_test where user_name = #{userName}
    </delete>

</mapper>


6:IUserService.java

package com.testProject.springboot.Service;

import java.util.List;

import com.testProject.springboot.entity.User;

public interface IUserService {

    
       List<User> getAllUsers();
       public int addUser(User user);
       public int deleteUser(User user);
}

 


7:UserServiceImpl.java 

package com.testProject.springboot.Service.impl;
//import java.sql.Date;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Primary;
import org.springframework.stereotype.Service;

import com.testProject.springboot.Mapper.UserMapper;
import com.testProject.springboot.Service.IUserService;
import com.testProject.springboot.entity.User;


//public class UserServiceImpl {
//
//}
@Service
@Primary
public class UserServiceImpl implements IUserService {

    @Autowired
    private UserMapper userMapper;

   // @Override
    public List<User> getAllUsers() {
        return userMapper.getAllUsers();
    }

   // @Override
    public int addUser(User user) {
        SimpleDateFormat form = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        user.setCreatedTime( form.format(new Date()) );
        return userMapper.addUser( user );
    }

  //  @Override
    public int deleteUser(User user) {
        return userMapper.deleteUser( user );
    }
}
 

运行测试 postman软件 

 

 

sqlserver 表结构

CREATE TABLE [dbo].[user_test](
    [user_id] [int] NOT NULL,
    [user_name] [varchar](50) NOT NULL,
    [sex] [tinyint] NOT NULL,
    [created_time] [varchar](50) NOT NULL
) ON [PRIMARY]

GO

SET ANSI_PADDING OFF
GO

 

结合上边和我里面的代码 。 代码是全部的,拷贝即可使用,为了和我一样的小白快速识别使用。

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

  • 0
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值