博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
第一个小demo: spring boot + mybatis + thymeleaf
阅读量:5163 次
发布时间:2019-06-13

本文共 6631 字,大约阅读时间需要 22 分钟。

整体项目结构

 

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)    })    List
getList(); @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 List
getList() { 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,运行效果

 

 代码下载地址 

转载于:https://www.cnblogs.com/cmbt/p/9477487.html

你可能感兴趣的文章
使用 Printf via SWO/SWV 输出调试信息
查看>>
.net 分布式架构之分布式锁实现(转)
查看>>
吴恩达机器学习笔记 —— 3 线性回归回顾
查看>>
Problem E: Automatic Editing
查看>>
SpringBoot 使用 MyBatis 分页插件 PageHelper 进行分页查询
查看>>
《DSP using MATLAB》Problem 6.17
查看>>
微信公众平台开发实战Java版之如何网页授权获取用户基本信息
查看>>
一周TDD小结
查看>>
sizeof与strlen的用法
查看>>
Linux 下常见目录及其功能
查看>>
开源框架中常用的php函数
查看>>
nginx 的提升多个小文件访问的性能模块
查看>>
set&map
查看>>
集合类总结
查看>>
4.AE中的缩放,书签
查看>>
CVE-2014-6321 && MS14-066 Microsoft Schannel Remote Code Execution Vulnerability Analysis
查看>>
给一次重新选择的机会_您还会选择程序员吗?
查看>>
Mysql MHA高可用集群架构
查看>>
心急的C小加
查看>>
编译原理 First,Follow,select集求法
查看>>