整体项目结构
1. 创建一个Maven web项目
2. 引入相关依赖
4.0.0 cmbt springbootdemo 1.0-SNAPSHOT war springbootdemo Maven Webapp http://www.example.com UTF-8 UTF-8 1.8 1.8 1.8 org.springframework.boot spring-boot-starter-parent 2.0.2.RELEASE junit junit 4.11 test org.springframework.boot spring-boot-starter-web org.springframework.boot spring-boot-starter-tomcat org.springframework.boot spring-boot-starter-thymeleaf org.mybatis.spring.boot mybatis-spring-boot-starter 1.3.2 mysql mysql-connector-java 5.1.21 org.springframework.boot spring-boot-devtools true true springbootdemo org.springframework.boot spring-boot-maven-plugin true maven-clean-plugin 3.0.0 maven-resources-plugin 3.0.2 maven-compiler-plugin 3.7.0 maven-surefire-plugin 2.20.1 maven-war-plugin 3.2.0 maven-install-plugin 2.5.2 maven-deploy-plugin 2.8.2
3. 新建一个 application.properties 文件
server.port=8000#thymeleaf startspring.thymeleaf.prefix=classpath:/templates/ spring.thymeleaf.mode=HTML5spring.thymeleaf.encoding=UTF-8spring.thymeleaf.content-type=text/html#开发时关闭缓存,不然没法看到实时页面(pom已经配了热部署插件,所以不用打开了)#spring.thymeleaf.cache=false#thymeleaf end#数据库配置spring.datasource.url=jdbc:mysql://数据库ip:3306/mydb?useUnicode=true&characterEncoding=utf-8spring.datasource.username=rootspring.datasource.password=rootspring.datasource.driver-class-name=com.mysql.jdbc.Driver
4. 创建数据库及表
CREATE DATABASE `mydb` /*!40100 DEFAULT CHARACTER SET latin1 */;CREATE TABLE `t_user` ( `user_id` int(11) NOT NULL AUTO_INCREMENT, `user_name` varchar(255) NOT NULL, `password` varchar(255) NOT NULL, `phone` varchar(255) NOT NULL, `student` tinyint(1) DEFAULT NULL, `sex` enum('MALE','FEMALE','SECRET') DEFAULT 'SECRET', PRIMARY KEY (`user_id`)) ENGINE=InnoDB AUTO_INCREMENT=1 DEFAULT CHARSET=utf8;
5. 建一个controller
@Controllerpublic class IndexController { private final static Logger logger = LoggerFactory.getLogger(IndexController.class); @Autowired private IUserService userService; @RequestMapping("/helloworld") public String helloworld() { return "helloworld"; } @ResponseBody //加了此注解就不会通过模板返回 @GetMapping("/getUser/{userId}") public User getUser(@PathVariable Integer userId) { User user = userService.getUser(userId); return user; } @GetMapping(value = "/hello") public String hello(Model model) { logger.info("logback 访问hello"); logger.error("logback 访问hello"); String name = "hello aaa"; model.addAttribute("name", name); return "hello"; }}
6. mapper文件
public interface UserMapper { @Select("SELECT * FROM t_user") @Results({ @Result(property = "userId", column = "user_id"), @Result(property = "userName", column = "user_name"), @Result(property = "password", column = "password"), @Result(property = "phone", column = "phone"), @Result(property = "student", column = "student", javaType = boolean.class), @Result(property = "sex", column = "sex", javaType = Sex.class) }) ListgetList(); @Select("SELECT * FROM t_user WHERE user_id = #{id}") @Results({ @Result(property = "userId", column = "user_id"), @Result(property = "userName", column = "user_name"), @Result(property = "password", column = "password"), @Result(property = "phone", column = "phone"), @Result(property = "student", column = "student", javaType = boolean.class), @Result(property = "sex", column = "sex", javaType = Sex.class) }) User getUser(Integer id); @Insert("INSERT INTO t_user(user_name, password, phone, student, sex) VALUES(#{userName}, #{password}, #{phone}, #{student}, #{sex})") void insert(User user); @Update("UPDATE t_user SET user_name=#{userName},password=#{password} WHERE user_id =#{userId}") void update(User user); @Delete("DELETE FROM t_user WHERE user_id =#{id}") void delete(Long id);}
7. service文件
@Service("userService")public class UserServiceImpl implements IUserService { @Autowired private UserMapper userDao; public ListgetList() { return userDao.getList(); } public User getUser(Integer id) { return userDao.getUser(id); } public void insert(User user){ userDao.insert(user); } public void update(User user){ userDao.update(user); } public void delete(Long id){ userDao.delete(id); }}
8. 打war包,部署到tomcat: mvn clean package
9. 启动tomcat,运行效果
代码下载地址