SpringBoot+Mybatis+Thymeleaf的简单示例

花了点时间做了SpringBoot+Mybatis+Thymeleaf的简单示例,同样是在SpringBoot学习记录(三)——集成Mybatis的基础上改的。只有用过之后,印象才能更加深刻,记录一下学习的过程,以备后续使用。

项目整体目录:

SpringBoot+Mybatis+Thymeleaf的简单示例_第1张图片

1、依赖引入


            org.springframework.boot
            spring-boot-starter-jdbc
        
        
            org.springframework.boot
            spring-boot-starter-thymeleaf
        
        
            org.springframework.boot
            spring-boot-starter-web
        
        
            org.mybatis.spring.boot
            mybatis-spring-boot-starter
            1.3.2
        

        
            mysql
            mysql-connector-java
        
        
            org.springframework.boot
            spring-boot-starter-test
            test
        

一般在创建的时候就选择好了。

2、配置文件

server.port=8080

#Thymeleaf配置
#禁用Thymeleaf的缓存
spring.thymeleaf.cache=false
spring.thymeleaf.encoding=utf-8
spring.thymeleaf.mode=LEGACYHTML5
spring.thymeleaf.prefix=classpath:/templates/
spring.thymeleaf.suffix=.html


#mapper文件的位置
mybatis.mapper-locations=classpath:mapper/*.xml
#对应实体类的位置
type-aliases-package=com.thr.entity 

#配置数据源
spring.datasource.username=root
spring.datasource.password=root
#使用druid数据源
#type: com.alibaba.druid.pool.DruidDataSource
spring.datasource.driver-class-name=com.mysql.cj.jdbc.Driver
spring.datasource.url=jdbc:mysql://127.0.0.1:3306/db_user?useUnicode=true&characterEncoding=utf8&useSSL=false&serverTimezone=GMT%2B8

使用thymeleaf的时候一般会禁用它的缓存,不然更新数据的时候页面还是那个数据。 

3、类

Controller类:

@Controller
public class UserController {

    @Autowired
    private UserService userService;

    //查所有
    @RequestMapping(value = "/list")
    public String list(Model model){
        List userList=userService.list();
        model.addAttribute("userList",userList);
        return "list";
    }

    //添加的跳转
    @RequestMapping(value = "/addIndex")
    public String addIndex(){
        return "add";
    }

    //添加
    @RequestMapping(value = "/add")
    public String add(User user){
            userService.add(user);
            return "redirect:/list";
    }

    //跳到修改页面
    @RequestMapping(value = "/toUpdate")
    public String toUpdate(Integer id, Model model){
        User user=userService.findById(id);
        model.addAttribute("user",user);
        return "update";
    }

    //修改
    @RequestMapping(value = "/udpate")
    public String update(User user){
        userService.update(user);
        return "redirect:/list";
    }

    //删除
    @RequestMapping(value = "/delete")
    public String delete(Integer id){
        userService.delete(id);
        return "redirect:/list";
    }
}

controller层使用普通的注解,没有使用@RestController。因为需要跳转页面,如果使用@RestController无法跳转页面。对外提供接口话,可以使用@RestController注解。

thymeleaf页面参数的传递可以使用这几种方式:request、ModelMap、Model

UserDao和UserService是一样的:

public interface UserDao {

    List list();

    int add(User user);

    int update(User user);

    int delete(Integer id);

    User findById(Integer id);
}

UserMapper.xml配置文件:



  
    
    
    
  

    

    
        insert into t_user values (null,#{name},#{password})
    

    
        update t_user set name=#{name},password=#{password} where id=#{id}
    

    
        delete from t_user where id=#{id}
    

    

实体:

public class User {
    private Integer id;
    private String name;
    private String password;

Service的实现:

@Service
public class UserServiceImpl implements UserService {

    @Autowired
    private UserDao userDao;

    @Override
    public List list() {
        return userDao.list();
    }

    @Override
    public int add(User user) {
        return userDao.add(user);
    }

    @Override
    public int update(User user) {
        return userDao.update(user);
    }

    @Override
    public int delete(Integer id) {
        return userDao.delete(id);
    }

    @Override
    public User findById(Integer id) {
        return userDao.findById(id);
    }

4、页面

list.html:



    add

    
id name password operation
name password edit delete

虽然thymeleaf用的是HTML页面,但是却拥有Jsp更加强大的功能。

add.html:



name
password

update.html:

name
password

5、运行

然后运行启动类访问http://localhost:8080/list,还有千万要记得在SpringBoot的启动类上面加一个@MapperScan注解,这样SpringBoot才能找到mapper(dao)接口,千万记住!!!!!

SpringBoot+Mybatis+Thymeleaf的简单示例_第2张图片

运行的效果如下,由于没有加CSS样式,可能看上去有点丑,但是里面的增删改查功能都是完整的。

SpringBoot+Mybatis+Thymeleaf的简单示例_第3张图片

你可能感兴趣的:(SpringBoot)