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> <groupId>springboot</groupId> <artifactId>springboot</artifactId> <version>1.0-SNAPSHOT</version> <packaging>war</packaging> <!--继承自Spring Boot stater parent--> <parent> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-parent</artifactId> <version>1.5.6.RELEASE</version> </parent> <!--依赖--> <dependencies> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> </dependency> <!--模板引擎jar包--> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-thymeleaf</artifactId> </dependency> <!--Mysql--> <dependency> <groupId>mysql</groupId> <artifactId>mysql-connector-java</artifactId> </dependency> <!--alibaba druid 数据库连接池,并且能够提供强大的监控和扩展功能。--> <dependency> <groupId>com.alibaba</groupId> <artifactId>druid</artifactId> <version>1.0.18</version> </dependency> <!--mybatis--> <!-- https://mvnrepository.com/artifact/org.mybatis.spring.boot/mybatis-spring-boot-starter --> <dependency> <groupId>org.mybatis.spring.boot</groupId> <artifactId>mybatis-spring-boot-starter</artifactId> <version>1.3.0</version> </dependency> <!--json--> <dependency> <groupId>com.alibaba</groupId> <artifactId>fastjson</artifactId> <version>1.2.21</version> </dependency> </dependencies> <build> <plugins> <plugin> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-maven-plugin</artifactId> </plugin> </plugins> </build> </project>
application.yml
server: port : 8080 context-path: /test spring: mvc: view: prefix: classpath:/templates/ suffix: .html datasource: url: jdbc:mysql://localhost:3306/study username: root password: qq123456 type: com.alibaba.druid.pool.DruidDataSource driver-class-name: com.mysql.jdbc.Driver filters: stat maxActive: 20 initialSize: 1 maxWait: 60000 minIdle: 1 timeBetweenEvictionRunsMillis: 60000 minEvictableIdleTimeMillis: 300000 validationQuery: select 'x' testWhileIdle: true testOnBorrow: false testOnReturn: false poolPreparedStatements: true maxOpenPreparedStatements: 20
Application
package com.study.hgf; import org.mybatis.spring.annotation.MapperScan; import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.EnableAutoConfiguration; import org.springframework.context.annotation.ComponentScan; /** * Created by hasee on 2017/6/25. */ @EnableAutoConfiguration @ComponentScan @MapperScan("com.study.hgf.dao.mapper") public class Application { public static void main(String[] args) { SpringApplication.run(Application.class,args); } }
User
package com.study.hgf.dao.bo; /** * Created by hasee on 2017/8/14. */ public class User { private Integer id_; private String name_; private String pass_; private Integer age_; public Integer getId_() { return id_; } public void setId_(Integer id_) { this.id_ = id_; } public String getName_() { return name_; } public void setName_(String name_) { this.name_ = name_; } public String getPass_() { return pass_; } public void setPass_(String pass_) { this.pass_ = pass_; } public Integer getAge_() { return age_; } public void setAge_(Integer age_) { this.age_ = age_; } }
UserMapper
package com.study.hgf.dao.mapper; import com.study.hgf.dao.bo.User; import org.apache.ibatis.annotations.Select; import java.util.List; /** * Created by hasee on 2017/8/14. */ public interface UserMapper { @Select("select id_,name_,pass_,age_ from users") public List<User> getUserList(); }
UserService
package com.study.hgf.service; import com.study.hgf.dao.bo.User; import com.study.hgf.dao.mapper.UserMapper; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Service; import java.util.List; /** * Created by hasee on 2017/8/14. */ @Service public class UserService { @Autowired private UserMapper userMapper; public List<User> getUserList(){ return userMapper.getUserList(); } }
LoginController
package com.study.hgf.controller; import com.alibaba.fastjson.JSON; import com.study.hgf.service.UserService; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Controller; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RequestMethod; import org.springframework.web.bind.annotation.ResponseBody; /** * Created by hasee on 2017/6/25. */ @Controller public class LoginController { @Autowired private UserService userService; @RequestMapping("/") @ResponseBody public String home(){ return JSON.toJSONString(userService.getUserList()); } }
运行结果