springboot学习三(springboot结合spring data jpa和freemarker显示)
2017-09-05 20:55
615 查看
springboot学习三(springboot结合spring data jpa和freemarker显示)
1.springboot结合springdatajpapom.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> 4000 <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 @GeneratedValue private 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 @Transactional public class UserServiceImpl implements IUserService{ @Autowired private UserJpaRepository userDao; @Override public List<User> findAll() { // TODO Auto-generated method stub return userDao.findAll(); } @Override public void saveUser(User book) { userDao.save(book); } @Cacheable("users") @Override public User findOne(int id) { System.out.println("Cached Pages"); return userDao.findOne((long) id); } @Override public void delete(int id) { userDao.delete((long) id); } @Override public 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 @Controller public class UserController { @Autowired private 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@36623adc zhangsan com.zl.entity.User@1b3b13ed lisi com.zl.entity.User@67808258 aa com.zl.entity.User@644bec06 曾龙 com.zl.entity.User@43d89e89 aa
相关文章推荐
- 【系统学习SpringBoot】SpringBoot初遇Spring-Data-JPA
- springboot 学习笔记【3】Spring Boot中使用Spring-data-jpa
- springboot学习-springboot使用spring-data-jpa操作MySQL数据库
- 深入学习spring-boot系列(二)--使用spring-data-jpa
- SpringBoot的Thymeleaf支持,结合SpringMVC做的案例(学习SpringBoot实战)
- 深入学习spring-boot系列(二)--使用spring-data-jpa
- SpringBoot学习-(4)集成SpringDataJpa
- 深入学习spring-boot系列(二)--使用spring-data-jpa
- Spring boot学习之spring-data-jpa的使用(二)
- 深入学习spring-boot系列(二)--使用spring-data-jpa
- 深入学习spring-boot系列(二)--使用spring-data-jpa
- 深入学习spring-boot系列(二)--使用spring-data-jpa
- 【SpringBoot学习笔记】SpringBoot_03_SpringData—JpaRepository部分字段查询功能
- 深入学习spring-boot系列(二)--使用spring-data-jpa
- springboot学习(5)springboot使用spring-data-jpa完成数据持久化
- Spring Boot学习进阶笔记(三)-Spring-data-jpa
- 【SpringBoot学习笔记】SpringBoot_01_SpringData—JpaRepository查询功能
- 深入学习spring-boot系列(二)--使用spring-data-jpa
- SpringBoot学习笔记 - 数据访问(Spring Data JPA)
- 深入学习spring-boot系列(二)--使用spring-data-jpa