1、依赖
<dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-test</artifactId> <scope>test</scope> </dependency> <!--启动时启动内置tomcat --> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-tomcat</artifactId> <scope>provided</scope> </dependency> <!--对Jsp支持 --> <dependency> <groupId>org.apache.tomcat.embed</groupId> <artifactId>tomcat-embed-jasper</artifactId> </dependency> <!-- 支持jstl --> <dependency> <groupId>javax.servlet</groupId> <artifactId>jstl</artifactId> </dependency> <!-- fastjson --> <dependency> <groupId>com.alibaba</groupId> <artifactId>fastjson</artifactId> <version>1.2.46</version> </dependency> <!-- MyBatis --> <dependency> <groupId>org.mybatis.spring.boot</groupId> <artifactId>mybatis-spring-boot-starter</artifactId> <version>1.1.1</version> </dependency> <!-- MySql驱动 --> <dependency> <groupId>mysql</groupId> <artifactId>mysql-connector-java</artifactId> </dependency> <dependency> <groupId>org.projectlombok</groupId> <artifactId>lombok</artifactId> <version>1.16.22</version> </dependency>
2、application.properties
#\u8BBE\u7F6ETomcat\u7AEF\u53E3\uFF0C\u9ED8\u8BA48080 server.port=8080 #\u8BBE\u7F6ETomcat\u7F16\u7801 server.tomcat.uri-encoding=UTF-8 #\u8BBE\u7F6E\u89C6\u56FE\u89E3\u6790\u5668\u8DEF\u5F84 spring.mvc.view.prefix=/WEB-INF/views/ #\u8BBE\u7F6E\u89C6\u56FE\u89E3\u6790\u5668\u540E\u7F00 spring.mvc.view.suffix=.jsp #\u6570\u636E\u5E93\u914D\u7F6E spring.datasource.url=jdbc:mysql://localhost:3306/licy?useUnicode=true&characterEncoding=utf-8&zeroDateTimeBehavior=convertToNull&allowMultiQueries=true spring.datasource.username=root spring.datasource.password=root spring.datasource.driver-class-name=com.mysql.jdbc.Driver #\u914D\u7F6E.xml\u6587\u4EF6\u8DEF\u5F84 mybatis.mapper-locations=classpath:mapper/*.xml #\u914D\u7F6E\u6A21\u578B\u8DEF\u5F84 mybatis.type-aliases-package=pom.domain
3、在resources目录下新建 mapper文件夹 并创建文件UserMapper.xml
<?xml version="1.0" encoding="UTF-8"?><!DOCTYPE mapper PUBLIC "-//ibatis.apache.org//DTD Mapper 3.0//EN" "http://ibatis.apache.org/dtd/ibatis-3-mapper.dtd"> <mapper namespace="pom.dao.UserDao"> <select id="getUserById" parameterType="pom.domain.User" resultType="pom.domain.User"> SELECT * FROM user WHERE ID = #{id} </select> </mapper>
4、dao
@Mapper public interface UserDao { User getUserById(User user); }
5、service
public interface UserService { User getUserById(User user); }
6、serviceImpl
@Service public class UserServiceImpl implements UserService{ @Autowired private UserDao userDao; @Override public User getUserById(User user) { return userDao.getUserById(user); } }
7、controller
@RestController // 表示该controller类下所有的方法都公用的一级上下文根 @RequestMapping(value = "/springboot") public class UserController { @Autowired private UserService userService; // 这里使用@RequestMapping注解表示该方法对应的二级上下文路径 @RequestMapping(value = "/getUserByGet", method = RequestMethod.GET) String getUserByGet(@RequestParam(value = "id") String id) { User user = new User(); user.setId(id); User u = userService.getUserById(user); return "Hello " + id; } }
8、访问
http://localhost:8080/springboot/getUserByGet?id=1