Springboot框架下的Thymeleaf和通用Mapper对数据库的 *** 作

Springboot框架下的Thymeleaf和通用Mapper对数据库的 *** 作,第1张

 首先导入依赖


            
            
                com.github.pagehelper
                pagehelper-spring-boot-starter
                1.2.10
            
            
            
                mysql
                mysql-connector-java
                5.1.46
            
            
            
                tk.mybatis
                mapper-spring-boot-starter
                2.1.5
            
            
            
                org.springframework.boot
                spring-boot-starter-jdbc
            
            
            
                org.springframework.boot
                spring-boot-starter-thymeleaf
            
            
            
                org.springframework.boot
                spring-boot-starter-web
            
            
            
                org.springframework.boot
                spring-boot-devtools
                runtime
                true
            
            
            
                org.springframework.boot
                spring-boot-configuration-processor
                true
            
            
            
                org.projectlombok
                lombok
                true
            
            
            
                org.springframework.boot
                spring-boot-starter-test
                test
                
                    
                        org.junit.vintage
                        junit-vintage-engine
                    
                
            
            
            
                org.springframework.boot
                spring-boot-starter-security
            
            
            
                org.springframework.security
                spring-security-test
                test
            
            
            
                org.thymeleaf.extras
                thymeleaf-extras-springsecurity5
            
        
MAPPER部分

public interface SanGuoMapperTK extends Mapper {

//selectAll
@Select(“select * from sanguo”)
List selectAll();
@Insert("insert into sanguo values(#{id},#{name},#{password},#{type},#{birth},#{headImg},#{sex})")
void addSanGuo(SanGuoTK sanGuoTK);

@Update("update sanguo set name=#{name},password=#{password},type=#{type},birth=#{birth},headImg=#{headImg},sex=#{sex} where id=#{id}")
void upDateSanGuo(SanGuoTK sanGuoTK);

@Select("select * from sanguo where id = #{id}")
List selectSanGuoByTd(int id);

@Select("select * from sanguo where id = #{id}")
SanGuoTK selectSanGuoByTd1(int id);

@Select("select * from sanguo where name like concat('%',#{name},'%')")
List selectSanGuoByName(String name);


}

}
这里的模糊查询比较坑,需要使用字符串拼接。不然会因为参数传递时会受到自己带有引号的影响。

CONTROLLER部分
@Controller
@RequestMapping("/user")
public class UserControall {

    @Autowired
    private SanGuoMapperTK sanGuoMapperTK;

    @RequestMapping("/showuser")//分页查询
    public String show(Model model,
                       @RequestParam(defaultValue = "1") Integer pageNo,
                       @RequestParam(defaultValue = "3") Integer pageSize){
        //1.分页查询
        PageHelper.startPage(pageNo,pageSize);
        List sanGuoTKS = sanGuoMapperTK.selectAll();
        PageInfo pageInfo = new PageInfo<>(sanGuoTKS);
        //2.存进model
        model.addAttribute("pageInfo",pageInfo);
        //3.跳转到users下的show页面
        return "admin/user/showuser";
    }



    @RequestMapping("/delete/{id}")//删除
    public String deleteById(@PathVariable Integer id){
        //使用rest风格接收参数,调用mapper实现删除,返回到index.html
        sanGuoMapperTK.deleteByPrimaryKey(id);
        return "admin/admin";
    }

    //新增用户:1.跳转到新增页面 2.执行用户数据新增
    @RequestMapping("/add_user")
    public String addUser(){
        return "admin/user/add_user";
    }

    @RequestMapping("/add")//用户添加
    public String addSanGuo(SanGuoTK sanGuoTK){
        System.out.println(sanGuoTK);
        sanGuoMapperTK.addSanGuo(sanGuoTK);
        return "redirect:/admin";
    }


    @RequestMapping("/update/{id}")
    public String updateSanGuo(Model model,@PathVariable Integer id){
        SanGuoTK sanGuoTK=sanGuoMapperTK.selectSanGuoByTd1(id);
        model.addAttribute("sanGuoTK",sanGuoTK);
        return "admin/user/update_user";
    }

    @RequestMapping("/updateUser")//用户更新
    public String updateSanGuo(SanGuoTK sanGuoTK){
        System.out.println(sanGuoTK);
        sanGuoMapperTK.upDateSanGuo(sanGuoTK);
        return "redirect:/admin";
    }

    @RequestMapping("/selectById")
    public String selectSanGuoById(Model model,SanGuoTK sanGuoTK,
                                   @RequestParam(defaultValue = "1") Integer pageNo,
                                   @RequestParam(defaultValue = "3") Integer pageSize){
        //1.分页查询
        PageHelper.startPage(pageNo,pageSize);
        List sanGuoTKS = sanGuoMapperTK.selectSanGuoByTd(sanGuoTK.getId());
        PageInfo pageInfo = new PageInfo<>(sanGuoTKS);
        //2.存进model
        model.addAttribute("pageInfo",pageInfo);
        //3.跳转到users下的show页面
        return "admin/user/showuser";
    }


    @RequestMapping("/selectByName")
    public String selectSanGuoByName(Model model,SanGuoTK sanGuoTK,
                                     @RequestParam(defaultValue = "1") Integer pageNo,
                                     @RequestParam(defaultValue = "3") Integer pageSize){
        //1.分页查询
        PageHelper.startPage(pageNo,pageSize);
        List sanGuoTKS = sanGuoMapperTK.selectSanGuoByName(sanGuoTK.getName());
        PageInfo pageInfo = new PageInfo<>(sanGuoTKS);
        //2.存进model
        model.addAttribute("pageInfo",pageInfo);
        //3.跳转到users下的show页面
        return "admin/user/showuser";
    }

}

实体类部分
@Data
@Table(name="sanguo")
public class SanGuoTK {
    @Id//主键
    @KeySql(useGeneratedKeys = true)//表示在insert语句中,可将自动生成的主键id返回
    private Integer id;
    private String name;
    private String password;
    private String type;
    @DateTimeFormat(pattern="yyyy-MM-dd")
    private Date birth;
    @Column(name = "headImg")
    private String headImg;
    private String sex;

}

这里的生日是data类型的数据,从前端获取的是String类型,需要注释才可以使用
table后面是数据库表名用于存放实体类的数据

HTML部分 ADMIN



    
    admin
    
    
    
    





菜单



    
        
            

个人信息维护

个人房源维护

个人房源维护

上面是完整的admin.HTML文件下面不展示完全,使用了前端框架这里附上链接http://amazeui.shopxo.net/

SHOWUSER

用户信息表

添加

编号 姓名 密码 生日 头像 等级 修改 删除
修改 删除

UPDATE_USER

返回

ADD_USER
返回

使用了较多的前端框架内容导致html文件过于臃肿
这是项目的结构图有一些在创建项目时会自动生成


执行的效果

数据库中的内容

img文件夹在static文件夹下

版权声明:本文为wjhyefei原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明。

本文链接:SpringBoot框架下的thymeleaf和通用Mapper对数据库的 *** 作_文嘉恒的博客-CSDN博客

欢迎分享,转载请注明来源:内存溢出

原文地址: https://outofmemory.cn/langs/719930.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-04-25
下一篇 2022-04-25

发表评论

登录后才能评论

评论列表(0条)

保存