springboot学习三(springboot结合spring data jpa和freemarker显示)
来源:互联网 发布:解毒丹 知乎 编辑:程序博客网 时间:2024/06/01 07:27
springboot学习三(springboot结合spring data jpa和freemarker显示)
1.springboot结合springdatajpa
pom.xml
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> <modelVersion>4.0.0</modelVersion> <groupId>com.zl</groupId> <artifactId>springboot-jpa</artifactId> <version>0.0.1-SNAPSHOT</version> <packaging>war</packaging> <name>springboot-jpa</name> <description/> <parent><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-parent</artifactId><version>1.5.2.RELEASE</version></parent> <properties> <webVersion>3.1</webVersion> <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding> </properties> <dependencies> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-data-jpa</artifactId> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-test</artifactId> <scope>test</scope> </dependency> <dependency> <groupId>mysql</groupId> <artifactId>mysql-connector-java</artifactId> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-cache</artifactId> </dependency> <!-- 引入freeMarker的依赖包. --> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-freemarker</artifactId> </dependency> </dependencies> <build> <plugins> <plugin> <artifactId>maven-compiler-plugin</artifactId> <version>2.3.2</version> <configuration> <source>1.8</source> <target>1.8</target> </configuration> </plugin> <plugin> <artifactId>maven-war-plugin</artifactId> <version>2.6</version> <configuration> <failOnMissingWebXml>false</failOnMissingWebXml> </configuration> </plugin> <plugin> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-maven-plugin</artifactId> </plugin> </plugins> </build></project>2.javabean
package com.zl.entity;import javax.persistence.Entity;import javax.persistence.GeneratedValue;import javax.persistence.Id;import javax.persistence.Table;/** * * @author: zenglong * @ClassName: User * @Description: 实体类 * @date: 2017年9月5日 */@Entity@Table(name="user")public class User {@Id@GeneratedValueprivate String uid;private String name;private int age;private String phone;private String passwd;public String getUid() {return uid;}public void setUid(String uid) {this.uid = uid;}public String getName() {return name;}public void setName(String name) {this.name = name;}public int getAge() {return age;}public void setAge(int age) {this.age = age;}public String getPhone() {return phone;}public void setPhone(String phone) {this.phone = phone;}public String getPasswd() {return passwd;}public void setPasswd(String passwd) {this.passwd = passwd;}}
3.service
package com.zl.service.impl;import java.util.List;import org.springframework.beans.factory.annotation.Autowired;import org.springframework.cache.annotation.Cacheable;import org.springframework.stereotype.Service;import org.springframework.transaction.annotation.Transactional;import com.zl.dao.UserJpaRepository;import com.zl.entity.User;import com.zl.service.IUserService;@Service@Transactionalpublic class UserServiceImpl implements IUserService{@Autowiredprivate UserJpaRepository userDao;@Overridepublic List<User> findAll() {// TODO Auto-generated method stubreturn userDao.findAll();}@Overridepublic void saveUser(User book) {userDao.save(book);}@Cacheable("users")@Overridepublic User findOne(int id) {System.out.println("Cached Pages");return userDao.findOne((long) id);}@Overridepublic void delete(int id) {userDao.delete((long) id);}@Overridepublic List<User> findByName(String name) { List<User> userList1 = userDao.findByName1(name); List<User> userList2 = userDao.findByName2(name); List<User> userList3 = userDao.findByNameAndUid(name, "3"); System.out.println("userList1:" + userList1); System.out.println("userList2:" + userList2); System.out.println("userList3:" + userList3); return userDao.findByName(name);}}
4.dao
package com.zl.dao;import java.util.List;import org.springframework.data.jpa.repository.JpaRepository;import org.springframework.data.jpa.repository.Query;import org.springframework.data.repository.query.Param;import com.zl.entity.User;/** * * @author: zenglong * @ClassName: UserJpaRepository * @Description: 数据持久层 * @date: 2017年9月5日 */public interface UserJpaRepository extends JpaRepository<User, Long> {List<User> findByNameAndUid(String name, String uid); @Query(value = "from User u where u.name=:name") List<User> findByName1(@Param("name") String name); @Query(value = "select * from #{#entityName} u where u.name=?1", nativeQuery = true) List<User> findByName2(String name); List<User> findByName(String name);}5.控制类
package com.zl.controller;import java.util.List;import javax.servlet.http.HttpSession;import org.springframework.beans.factory.annotation.Autowired;import org.springframework.stereotype.Controller;import org.springframework.ui.ModelMap;import org.springframework.web.bind.annotation.PathVariable;import org.springframework.web.bind.annotation.RequestMapping;import org.springframework.web.bind.annotation.RestController;import org.springframework.web.servlet.ModelAndView;import com.zl.entity.User;import com.zl.service.IUserService;//@RestController@Controllerpublic class UserController {@Autowiredprivate IUserService userService;@RequestMapping(value = "/add/{age}/{name}/{address}/{passwd}") public User addUser(@PathVariable String passwd,@PathVariable int age, @PathVariable String name, @PathVariable String phone) { User user = new User(); user.setAge(age); user.setName(name); user.setPhone(phone); user.setPasswd(passwd); userService.saveUser(user); return user; } @RequestMapping(value = "/delete/{id}") public void deleteBook(@PathVariable int id) { userService.delete(id); }// @RequestMapping(value = "/")// public String getBooks(ModelMap model)// {// List<User> user = userService.findAll();// model.addAttribute("user",user);// return "MyHtml";// }
//两种方式传值 @RequestMapping(value = "/") public ModelAndView getBooks() { List<User> user = userService.findAll(); ModelAndView model=new ModelAndView("MyHtml");// ModelAndView model=new ModelAndView("MyHtml","user",user);//或者这样传值 model.addObject("user",user); return model; } @RequestMapping(value = "/{id}") public User getUser(@PathVariable int id) { User user = userService.findOne(id); return user; } @RequestMapping(value = "/search/name/{name}") public List<User> getBookByName(@PathVariable String name) { List<User> users = userService.findByName(name); return users; }}6.ftl
<!DOCTYPE html><html> <head> <title>MyHtml.ftl</title> <meta name="keywords" content="keyword1,keyword2,keyword3"> <meta name="description" content="this is my page"> <meta name="content-type" content="text/html; charset=UTF-8"> <!--<link rel="stylesheet" type="text/css" href="./styles.css">--> </head> <body> <#list user as item> ${item}<br/> ${item.name}<br/> </#list> </body></html>7.访问:http://localhost:8080/
可以看到如下显示
com.zl.entity.User@36623adczhangsancom.zl.entity.User@1b3b13edlisicom.zl.entity.User@67808258aacom.zl.entity.User@644bec06曾龙com.zl.entity.User@43d89e89aa
阅读全文
0 0
- springboot学习三(springboot结合spring data jpa和freemarker显示)
- 【系统学习SpringBoot】SpringBoot初遇Spring-Data-JPA
- SpringBoot学习(五)操作数据库Spring-Data-JPA
- springboot学习(5)springboot使用spring-data-jpa完成数据持久化
- springboot整合spring data jpa
- springboot集成spring-data-jpa
- springboot学习笔记(三)使用JDBC以及整合spring data jpa
- Springboot学习笔记(三)JPA-Hibernate
- SpringBoot 学习记录(三): jpa
- springboot(五):spring data jpa的使用
- springboot整合swagger UI 、spring-data-JPA
- springBoot+easyui +spring data JPA 实现分页
- SpringBoot(五) :spring data jpa 的使用
- springboot(五):spring data jpa的使用
- springboot中使用spring -data-jpa (一)
- springboot中使用spring -data-jpa (二)
- springboot(五):spring data jpa的使用
- springboot(五):spring data jpa的使用
- 【Mybatis升级版-03】Controller方法的返回值
- idea创建springcloud项目图文教程(Feign实现负载均衡)(九)
- 《UNIX网络编程 卷2》 笔记: Posix消息队列(2)
- 枚举排列
- C语言内存管理函数总结(1)
- springboot学习三(springboot结合spring data jpa和freemarker显示)
- vue 页面跳转,传数据
- FreeRTOS列表和列表项
- npm包change-version的编写
- collections 模块整理
- ssm练手(CRUD) 2、修改sql方法,并利用JUnit结合数据库进行测试
- HDU 6191 DFS序+可持久化字典树
- 文章标题
- 实训开始!~~