使用springboot+ssm框架,在SQLServer数据库中查数据
pom.xml
<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> <groupId>com.hzx</groupId> <artifactId>testmaven03</artifactId> <version>0.0.1-SNAPSHOT</version> <packaging>pom</packaging> <parent> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-parent</artifactId> <version>2.3.0.RELEASE</version> </parent> <dependencies> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter</artifactId> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> </dependency> <dependency> <groupId>com.microsoft.sqlserver</groupId> <artifactId>mssql-jdbc</artifactId> </dependency> <dependency> <groupId>com.alibaba</groupId> <artifactId>druid-spring-boot-starter</artifactId> <version>1.1.23</version> </dependency> <dependency> <groupId>com.microsoft.sqlserver</groupId> <artifactId>sqljdbc4</artifactId> <version>4.0.0</version> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-jdbc</artifactId> </dependency> <dependency> <groupId>junit</groupId> <artifactId>junit</artifactId> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-test</artifactId> </dependency> <dependency> <groupId>com.alibaba</groupId> <artifactId>druid</artifactId> <version>1.1.23</version> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-devtools</artifactId> <optional>true</optional> <scope>true</scope> </dependency> <dependency> <groupId>org.mybatis.spring.boot</groupId> <artifactId>mybatis-spring-boot-starter</artifactId> <version>2.1.3</version> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-json</artifactId> </dependency> </dependencies> <build> <resources> <resource> <directory>src/main/java</directory> <includes> <include>/*.yml</include> <include>/*.properties</include> <include>/*.xml</include> </includes> <filtering>false</filtering> </resource> <resource> <directory>src/main/resources</directory> <includes> <include>/*.yml</include> <include>/*.properties</include> <include>/*.xml</include> </includes> <filtering>false</filtering> </resource> </resources> </build> </project>
application.yml
spring: datasource: driver-class-name: com.microsoft.sqlserver.jdbc.SQLServerDriver url: jdbc:sqlserver://127.0.0.1:1433;DatabaseName=test01 username: root password: devtools: restart: enabled: true additional-paths: src/main/java exclude: WEB-INF/ freemarker: cache: false
testmaven03.com.hzx.pojo;
@JsonInclude(value = JsonInclude.Include.NON_NULL) public class User {
private int id; private String name; private String password; public User() {
super(); } public User(int id, String name, String password) {
super(); this.id = id; this.name = name; this.password = password; } public int getId() {
return id; } public void setId(int id) {
this.id = id; } public String getName() {
return name; } public void setName(String name) {
this.name = name; } public String getPassword() {
return password; } public void setPassword(String password) {
this.password = password; } @Override public String toString() {
return "User [id=" + id + ", name=" + name + ", password=" + password + "]"; } }
testmaven03.com.hzx.dao
@Mapper public interface UserMapper {
List<User> queryAllUsers(); }
<?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="testmaven03.com.hzx.dao.UserMapper"> <resultMap id="UserMap" type="testmaven03.com.hzx.pojo.User"> <id property="id" column="id"/> <result property="name" column="name"/> <result property="password" column="password"/> </resultMap> <select id="queryAllUsers" parameterType="testmaven03.com.hzx.pojo.User" resultMap="UserMap"> select * from user_table; </select> </mapper>
testmaven03.com.hzx.service
public interface UserService {
List<User> queryAllUsers(); }
testmaven03.com.hzx.service.impl
@Service public class UserServiceImpl implements UserService{
@Autowired private UserMapper userMapper; @Override public List<User> queryAllUsers() {
return userMapper.queryAllUsers(); } }
testmaven03.com.hzx.bean
public class AjaxMessage {
private boolean status; private String message; private Object result; public AjaxMessage() {
super(); } public AjaxMessage(boolean status, String message, Object result) {
super(); this.status = status; this.message = message; this.result = result; } public boolean isStatus() {
return status; } public void setStatus(boolean status) {
this.status = status; } public String getMessage() {
return message; } public void setMessage(String message) {
this.message = message; } public Object getResult() {
return result; } public void setResult(Object result) {
this.result = result; } }
testmaven03.com.hzx.controller
@RestController @RequestMapping("/user") public class UserController {
@Autowired private UserService userService; @RequestMapping("/getuser") public AjaxMessage getUserList() {
List<User> userList = userService.queryAllUsers(); if (userList == null) {
return new AjaxMessage(false, "没有数据", null); } else {
return new AjaxMessage(true, "", userList); } } }
启动类
@SpringBootApplication public class StartApplication {
public static void main(String[] args) throws Exception {
SpringApplication.run(StartApplication.class, args); } }