项目完整图
首先pom
<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/maven-v4_0_0.xsd"> <modelVersion>4.0.0</modelVersion> <groupId>com.pwl</groupId> <artifactId>SpringBoot-01</artifactId> <packaging>war</packaging> <version>0.0.1-SNAPSHOT</version> <name>SpringBoot-01 Maven Webapp</name> <url>http://maven.apache.org</url> <parent> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-parent</artifactId> <version>2.0.1.RELEASE</version> </parent> <dependencies> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> </dependency> <dependency> <groupId>org.mybatis.spring.boot</groupId> <artifactId>mybatis-spring-boot-starter</artifactId> <version>1.3.1</version> </dependency> <dependency> <groupId>mysql</groupId> <artifactId>mysql-connector-java</artifactId> </dependency> <dependency> <groupId>com.alibaba</groupId> <artifactId>druid</artifactId> <version>1.0.5</version> </dependency> <dependency> <groupId>com.alibaba</groupId> <artifactId>fastjson</artifactId> <version>1.2.38</version> </dependency> </dependencies> <build> <plugins> <plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-compiler-plugin</artifactId> <configuration> <source>1.8</source> <target>1.8</target> </configuration> </plugin> </plugins> </build> </project>
创建application.properties
# mybatis mybatis.type-aliases-package=com.pwl.springboot.dao mybatis.configuration.map-underscore-to-camel-case=true mybatis.configuration.default-fetch-size=100 mybatis.configuration.default-statement-timeout=3000 mybatis.mapperLocations = classpath:com/pwl/springboot/dao/*.xml # druid spring.datasource.url=jdbc:mysql://127.0.0.1:3306/springboot?useUnicode=true&characterEncoding=utf-8&allowMultiQueries=true&useSSL=false spring.datasource.username=root spring.datasource.password=root spring.datasource.driver-class-name=com.mysql.jdbc.Driver spring.datasource.type=com.alibaba.druid.pool.DruidDataSource spring.datasource.filters=stat spring.datasource.maxActive=2 spring.datasource.initialSize=1 spring.datasource.maxWait=60000 spring.datasource.minIdle=1 spring.datasource.timeBetweenEvictionRunsMillis=60000 spring.datasource.minEvictableIdleTimeMillis=300000 spring.datasource.validationQuery=select 'x' spring.datasource.testWhileIdle=true spring.datasource.testOnBorrow=false spring.datasource.testOnReturn=false spring.datasource.poolPreparedStatements=true spring.datasource.maxOpenPreparedStatements=20
在com.pwl.springboot.entity包下创建User实体
package com.pwl.springboot.entity; public class User { private int id; private String name; 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; } @Override public String toString() { return "User [id=" + id + ", name=" + name + "]"; } }
创建com.pwl.springboot.dao包并创建UserDao接口
UserDao.java
package com.pwl.springboot.dao; import org.apache.ibatis.annotations.Mapper; import org.apache.ibatis.annotations.Param; import org.apache.ibatis.annotations.Select; import com.pwl.springboot.entity.User; @Mapper public interface UserDao { @Select("select * from User where id=#{id}") public User select(@Param("id")int id); }
依次创建service
创建com.pwl.springboot.service包并创建接口
UserService.java接口
package com.pwl.springboot.service; import com.pwl.springboot.entity.User; public interface UserService { public User selectUser(int id); }
实现类
创建com.pwl.springboot.service.impl包并创建UserServiceImpl实现类
UserServiceImpl.java
package com.pwl.springboot.service.impl; import javax.annotation.Resource; import org.springframework.stereotype.Service; import com.pwl.springboot.dao.UserDao; import com.pwl.springboot.entity.User; import com.pwl.springboot.service.UserService; @Service public class UserServiceImpl implements UserService{ @Resource private UserDao userDao; @Override public User selectUser(int id) { return userDao.select(id); } }
然后创建UserController.java
package com.pwl.springboot.controller; import javax.annotation.Resource; import org.springframework.stereotype.Controller; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.ResponseBody; import com.alibaba.fastjson.JSON; import com.pwl.springboot.entity.User; import com.pwl.springboot.service.UserService; @Controller @RequestMapping("/") public class UserController { @Resource private UserService userService; @RequestMapping("getUser") @ResponseBody public String getUser(int id) { User user = userService.selectUser(id); return JSON.toJSONString(user); } }
在创建com.pwl.springboot包并创建springboot启动类
package com.pwl.springboot; import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; @SpringBootApplication public class MainApplication { public static void main(String[] args) { SpringApplication.run(MainApplication.class, args); } }
启动并访问项目