springboot整合springDataJPA(替代了MyBatis)

SpringDataJPA不需要表直接使用注解,是对JPA的封装。

需求:查询数据库---->查到数据---->展示到页面上

分析:

  1.创建数据表:user表(此处建表,只是为了依赖导入失败,注解无法使用时备用)

DROP TABLE IF EXISTS `user`;
CREATE TABLE `user` (
	`id` int(11) NOT NULL AUTO_INCREMENT,
	`username` varchar(50) DEFAULT NULL,
	`password` varchar(50) DEFAULT NULL,
	`name` varchar(50) DEFAULT NULL,
	PRIMARY KEY (`id`)
	) ENGINE=InnoDB AUTO_INCREMENT=10 DEFAULT CHARSET=utf8;

  插入数据:

INSERT INTO `user` VALUES (‘1‘, ‘zhangsan‘, ‘123‘, ‘张wujiji);
INSERT INTO `user` VALUES (‘2‘, ‘lisi‘, ‘123‘, ‘李四‘);

  2.使用持久层框架SpringDataJPA

    1.导入依赖

<!-- springBoot JPA的起步依赖 -->
<dependency>
	<groupId>org.springframework.boot</groupId>
	<artifactId>spring-boot-starter-data-jpa</artifactId>
</dependency>
<!--数据库的依赖-->
<dependency>
	<groupId>mysql</groupId>
	<artifactId>mysql-connector-java</artifactId>
</dependency>

    2.创建实体类

@Entity                  //声明此类是实体类
@Table(name = "user")    //声明表名为user
@Data                    //此注解作用相当于添加了setter、getter、toString方法
public class User {
	//主键自增长策略
	@Id              //指定的类的属性,用于识别(一个表中的主键)
        //指定如何标识属性可以被初始化,例如自动、手动、或从序列表中获得的值
	@GeneratedValue(strategy = GenerationType.IDENTITY)
	private int id;

	private String username;
	private String password;
	private String name;
}

    3.创建Controller类

@RestController         //相当于@Controller和@RequestMapping两个注解的作用
public class UserController {

	@Autowired
	private UserDao userDao;

	@RequestMapping("/user/list")
	public List<User> getUserList(){
		return userDao.findAll();    //方法可以直接调用,不需要自己手写
	}
}

    4.创建dao层:

//采用SpringDataJPA的方式必须继承JpaRepository,两个泛型,一个是实体类的类型,另一个是主键的类型
public interface UserDao extends JpaRepository<User,Integer> {
}

  3.将查询到的数据显示到页面上

  将数据显示到页面上的方式有:

    1.静态:html、vue.js框架、angular.js框架(此框架出bug时不会报出在第几行,需要一行一行的找),后两个都是重型框架,加载速度慢,只适用于写办公软件、后台等被公司内部员工所用,不适用于客户

    2.动态:每次请求都生成一次页面jsp、json、freemarker模板、tymeleaf模板(比较麻烦)、velocity框架(已过时)。

    jsp本质上就是servlet,运行时需要容器,即工程,切必须是web工程,项目中若使用了springboot框架,则项目中不推荐使用jsp

  此处我们使用模板技术freemarker:

  freemarker使用步骤:

      1.添加依赖
<!--freemarker模板依赖-->
<dependency>
	<groupId>org.springframework.boot</groupId>
	<artifactId>spring-boot-starter-freemarker</artifactId>
</dependency>
      2.创建模板文件,保存位置在resources/templates目录下,文件后缀名为.ftl
<html>    <head>        <title>spring boot</title>    </head>    <body>        <table border="1px">            <thead>            <tr>                <th>id</th>                <th>用户名</th>                <th>密码</th>                <th>姓名</th>            </tr>            </thead>            <tbody>                <#--循环 将后台返回回来的userList数据循环 user代表userList中的单个对象-->                <#--循环次数有userList中的数据决定,有多少条就循环多少次-->                <#list userList as user>                    <tr>                        <th>${user.id}</th>                        <th>${user.username}</th>                        <th>${user.password}</th>                        <th>${user.name}</th>                    </tr>                </#list>            </tbody>        </table>    </body></html>
      3.编写Controller类,把结果传递给模板
@Controller
public class PageController {
    @Autowired
    private UserDao userDao;

    @Value("${page.rows}")
    private Integer rows;
    @RequestMapping("/page/user/list")
    public String getUserList(Model model){
        List<User> userList = userDao.findAll();
        model.addAttribute("userList",userList);
        return "user";//返回ftl的名字,跳转到页面
    }
}

原文地址:https://www.cnblogs.com/zhy819/p/11808656.html

时间: 2024-08-30 10:01:25

springboot整合springDataJPA(替代了MyBatis)的相关文章

Springboot整合SpringDataJpa入门篇(一)

spring-data-Jpa简介                                                                                                                                                                                                                                         

七、springboot整合Spring-data-jpa(二)之通用DAO接口与添加自定义方法

@NoRepositoryBean:Spring Data Jpa在启动时就不会去实例化BaseRepository这个接口 1.通用接口: import org.springframework.data.jpa.repository.JpaRepository; import org.springframework.data.jpa.repository.JpaSpecificationExecutor; import org.springframework.data.repository.N

SpringBoot 2.SpringBoot整合Mybatis

一.创建Springboot的配置文件:application.properties SpringApplication 会从 application.properties 文件中加载配置信息,下面是添加Spring配置信息的文件目录顺序: 当前目录下的/config子目录中 当前目录中 一个 classpath 包下的 /config 目录中 classpath 根目录中 大家根据自己习惯来即可. /application.properties 文件配置如下: spring.datasourc

springboot整合mybatis,freemarker

springboot 整合mybaits,,freemarker pom.xml文件 <?xml version="1.0" encoding="UTF-8"?><project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation=&

springboot整合mybatis(SSM开发环境搭建)

0.项目结构: 1.application.properties中配置整合mybatis的配置文件.mybatis扫描别名的基本包与数据源 server.port=80 logging.level.org.springframework=DEBUG #springboot mybatis #jiazai mybatis peizhiwenjian mybatis.mapper-locations = classpath:mapper/*Mapper.xml mybatis.config-loca

SpringBoot整合Mybatis多数据源 (AOP+注解)

SpringBoot整合Mybatis多数据源 (AOP+注解) 1.pom.xml文件(开发用的JDK 10) <?xml version="1.0" encoding="UTF-8"?> <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:s

springboot整合mybatis(注解)

springboot整合mybatis(注解) 1.pom.xml: <?xml version="1.0" encoding="UTF-8"?> <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="ht

SpringBoot整合mybatis、shiro、redis实现基于数据库的细粒度动态权限管理系统

1.前言本文主要介绍使用SpringBoot与shiro实现基于数据库的细粒度动态权限管理系统实例. 使用技术:SpringBoot.mybatis.shiro.thymeleaf.pagehelper.Mapper插件.druid.dataTables.ztree.jQuery 开发工具:intellij idea 数据库:mysql.redis 2.表结构还是是用标准的5张表来展现权限.如下图:image 分别为用户表,角色表,资源表,用户角色表,角色资源表.在这个demo中使用了mybat

SpringBoot整合mybatis踩坑

springboot整合mybaits过程中,调用接口时报错:org.apache.ibatis.binding.BindingException: Invalid bound statement (not found): 分析了下报错堆栈信息,认为是找不到*Mapper.xml导致,网上搜索下他人博客,以为是IDEA导致*Mapper.xml无法生成,于是检查了编译生成的classes目录(classpath),发现*Mapper.xml是存在的,IDEA并没有问题. 就在百思不得其解时,仔细