Spring boot实现数据库表格的生成、筛选、下载、打印功能——项目实战经验

Spring boot实现数据库表格的生成、筛选、下载、打印功能

这里介绍一种方法使用 layui框架table.render()组件

效果预览


一、前端

1、body部分添加一行代码

 <!-- 引入 layui.css -->
    <link rel="stylesheet" href="//unpkg.com/layui@2.6.6/dist/css/layui.css">


<!--body 部分添加一行代码-->
<table class="layui-hide" id="demo" lay-filter="test"></table>

2、引入layui框架js,实例化table

 

 引入 layui.js -->
    <script src="//unpkg.com/layui@2.6.6/dist/layui.js"></script>
<script>
layui.use(function(){ 
  //得到各种内置组件
  table = layui.table; //表格
 //执行一个 table 实例
  table.render({
    elem: '#demo'
    ,height: 420
    ,url: '/product?token=09' //换成自己的的数据接口
    ,title: '产品表'
    ,page: false//开启分页
    ,toolbar: 'default' //开启工具栏,此处显示默认图标,可以自定义模板,详见文档
    ,totalRow: false//开启合计行
    ,cols: [ //在spring boot环境下必须是这种层次结构,
    	[ //表头
    	{type: 'checkbox', fixed: 'left'}
        ,{field: 'id', title: 'ID', width:80, sort: true,fixed: 'left'}
        ,{field: 'sort', title: '类别', width:80, sort: true}
        ,{field: 'name', title: '名称', width: 200 }
        ,{field: 'price', title: '单价', width: 80, sort: true}
        ,{field: 'status', title: '状态', width:80, sort: true}
        ,{field: 'image_url', title: '图片地址', width: 200}
        ,{field: 'create_time', title: '添加时间', width: 170, sort: true}
        ,{field: 'update_time', title: '更新时间', width:170, sort: true} 
        ,{field: 'delete', title: '删除时间', width: 170, sort: true}

    ]
    	]
  });
});
</script>

二、业务端

1、Controller层请求接口

@Autowired
	private ProductRepository productRepository;
	@GetMapping("/producttest")
	public String productTest(HttpServletResponse response, @RequestParam("token") String token) {
		LinkedHashMap<String, Object> map = new LinkedHashMap<String, Object>();
		//放入layui框架fuction()返回参数
		map.putIfAbsent("code", "0");
		map.putIfAbsent("msg", "success");
		map.putIfAbsent("count", "100");
		map.putIfAbsent("data", productRepository.findAll());
		System.out.println(map);
		return JSON.toJSONString(map);
	}

2、DAO层

@Repository
public interface ProductRepository extends JpaRepository<Product,Integer> {

	
}

3、Module层

@Entity(name="product")
public class Product {
	@Id
	@GeneratedValue(strategy=GenerationType.IDENTITY)
	private Integer id;
	
	@Column()
	private String sort;
	private String name;
	private double price;
	private String image_url;
	private String status;
	private String create_time;
	private String update_time;
	private String delete_time;

    //此处省略了get,set方法,需要自己自动生成
}

 

三、需要引入的依赖

    <!-- json格式转换 --> 
        <dependency> 
            <groupId>com.alibaba</groupId> 
            <artifactId>fastjson</artifactId> 
            <version>1.2.47</version> 
        </dependency> 
    <!-- 添加Spring Data JPA依赖启动器 -->
		<dependency>
			<groupId>org.springframework.boot</groupId>
			<artifactId>spring-boot-starter-data-jpa</artifactId>
		</dependency>

四、具体效果

1、生成、筛选

2、下载

    

3、打印

  • 2
    点赞
  • 9
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
首先,你需要在 Spring Boot 中配置数据库连接。这可以通过在 `application.properties` 文件中添加以下行来完成: ``` spring.datasource.url=jdbc:mysql://localhost:3306/your_database spring.datasource.username=your_username spring.datasource.password=your_password spring.datasource.driver-class-name=com.mysql.jdbc.Driver ``` 请将 `your_database` 替换为你的数据库名称,`your_username` 替换为你的数据库用户名,`your_password` 替换为你的数据库密码。 接下来,你需要创建一个模型类,用于表示你的数据库表中的数据。例如,如果你要搜索书籍,你可以创建一个名为 `Book` 的类,其中包含书籍的属性(如书名、作者、出版社等)。 然后,你需要创建一个 `BookRepository` 接口,用于从数据库中检索和保存 `Book` 对象。你可以使用 Spring Data JPA 来生成 `BookRepository` 接口,如下所示: ```java public interface BookRepository extends JpaRepository<Book, Long> { List<Book> findByTitleContainingOrAuthorContainingOrPublisherContaining(String title, String author, String publisher); } ``` 在上面的代码中,我们使用 `findByTitleContainingOrAuthorContainingOrPublisherContaining` 方法来实现搜索功能。该方法将使用 `LIKE` 运算符从数据库中检索包含指定关键字的书籍。 最后,你可以在你的控制器中处理搜索请求,并将结果返回给用户。例如: ```java @RestController public class BookController { @Autowired private BookRepository bookRepository; @GetMapping("/search") public List<Book> search(@RequestParam("q") String query) { List<Book> books = bookRepository.findByTitleContainingOrAuthorContainingOrPublisherContaining(query, query, query); return books; } } ``` 在上面的代码中,我们使用 `@GetMapping` 注解来处理 HTTP GET 请求,`@RequestParam` 注解来获取搜索关键字,然后调用 `BookRepository` 中的 `findByTitleContainingOrAuthorContainingOrPublisherContaining` 方法来执行搜索。最后,我们将搜索结果作为 JSON 数组返回给用户。 以上就是使用 Spring Boot 连接数据库实现搜索功能的基本步骤。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值