SpringBoot+SQLServer2008R2+Mybatis开发后端
SpringBoot3+SQLServer2008R2+Mybatis开发后端
·
1. pox.xml
<parent>
<artifactId>spring-boot-starter-parent</artifactId>
<groupId>org.springframework.boot</groupId>
<version>3.3.4</version>
</parent>
<!-- web起步依赖 -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<!-- sqlserver,完全成功 -->
<dependency>
<groupId>com.microsoft.sqlserver</groupId>
<artifactId>mssql-jdbc</artifactId>
<version>8.4.1.jre8</version>
</dependency>
<!-- mybatis -->
<dependency>
<groupId>org.mybatis.spring.boot</groupId>
<artifactId>mybatis-spring-boot-starter</artifactId>
<version>3.0.3</version>
</dependency>
2. application.yml
#使用Microsoft官方的SQL Server JDBC驱动连接
spring:
datasource:
driver-class-name: com.microsoft.sqlserver.jdbc.SQLServerDriver
url: jdbc:sqlserver://127.0.0.1:1433;DatabaseName=******
password: ******
username: sa
3.1 Mapper
<?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="org.******.mapper.UserMapper">
<!--上方alt+ctrl+空格调出提示,必须是对应接口的全类名-->
<!--动态SQL-->
<select id="userInfo" resultType="org.******.pojo.User">
select row_id,user_id,username,password,pinyin,sex,id_card,education,school,phone,we_chat,email,user_level,job,department,his_code,avatar,create_time,role,creator
from userinfo
<where>
<if test="userId!=null">
user_id=#{userId}
</if>
<if test="username!=null">
and username=#{username}
</if>
and 1=1
</where>
</select>
</mapper>
3.2 Mapper
package org.******.mapper;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Select;
import org.******.pojo.User;
@Mapper
public interface UserMapper {
//映射配置文件,可以从官网或百度搜索获取,与mapper接口目录和文件名要保持一致
User userInfo(String userId, String username);
}
4. Controller
package org.******.Controller;
import org.******.pojo.Result;
import org.******.pojo.User;
import org.******.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
@RestController
@RequestMapping("/user")
public class UserController {
@Autowired
private UserService userService;
@PostMapping("/info")
public Result<User> userinfo() {
String userId = "001";
String username = "测试1";
User u = userService.userInfo(userId,username);
if (u==null){
return Result.error("根据提供条件没有查到信息。");
}else {
return Result.success(u);
}
}
}
5.1 Service
package org.******.service;
import org.******.pojo.User;
public interface UserService {
User userInfo(String userId, String username);
}
5.2 Service
package org.******.service.impl;
import org.******.mapper.UserMapper;
import org.******.pojo.User;
import org.******.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
@Service
public class UserServiceImpl implements UserService {
@Autowired
private UserMapper userMapper;
@Override
public User userInfo(String userId, String username) {
User u = userMapper.userInfo(userId,username);
return u;
}
}
6. pojo
package org.healthsystem.pojo;
import com.fasterxml.jackson.annotation.JsonFormat;
import com.fasterxml.jackson.annotation.JsonIgnore;
import jakarta.validation.constraints.Email;
import jakarta.validation.constraints.NotEmpty;
import jakarta.validation.constraints.NotNull;
import jakarta.validation.constraints.Pattern;
import lombok.Data;
import java.time.LocalDateTime;
//lombok 在编译阶段,为实体类自动生成setter getter toString
//pom文件引入依赖 在实体类上添加注解
@Data
public class User {
// @NotNull
private Integer rowId;//主键ID
// @NotNull
private Integer userId;//用户ID
private String username;//用户名
@JsonIgnore //让springmvc把当前对象转化成json字符串的时候,忽略password,最终的字符串中就没有password这个属性了
private String password;//密码
// @NotEmpty
// @Pattern(regexp = "^\\S{1,10}$")
private String pinyin;//拼音
private String sex;
private String idCard;
private String education;
private String school;
private String phone;
private String weChat;
// @NotEmpty
// @Email
private String email;//邮箱
private String userLevel;
private String job;
private String department;
private String hisCode;
private String avatar;//用户头像地址
private String role;
private LocalDateTime createTime;//创建时间
private String creator;//创建者
}
更多推荐
所有评论(0)