Springboot整合freemarker和jpa

首先打开idea新建一个springboot项目:file-new project
Springboot整合freemarker和jpa_第1张图片

然后点击next
Springboot整合freemarker和jpa_第2张图片
分别选中web ->web TemplateEngine-> freemarker SQL->jpa
然后next ->finish
pom文件如下`

4.0.0

com.example
springboot
0.0.1-SNAPSHOT
jar

springboot
Demo project for Spring Boot


	org.springframework.boot
	spring-boot-starter-parent
	2.0.4.RELEASE
	 



	UTF-8
	UTF-8
	1.8



	
		org.springframework.boot
		spring-boot-starter-web
	

	
		org.springframework.boot
		spring-boot-starter-test
		test
	
	
	
		javax.servlet
		javax.servlet-api
		provided
	
	
		javax.servlet
		jstl
	
	
		org.springframework.boot
		spring-boot-starter-tomcat
	
	
		org.apache.tomcat.embed
		tomcat-embed-jasper
		provided
	
	
		org.springframework.boot
		spring-boot-starter-data-jpa
		2.0.4.RELEASE
	
	
		mysql
		mysql-connector-java
		5.1.46
	
	
		org.elasticsearch.client
		transport
		5.5.2
	
	
		com.alibaba
		fastjson
		1.2.46
	
	
		org.codehaus.jackson
		jackson-core-asl
		1.9.13
	
	
		org.codehaus.jackson
		jackson-mapper-asl
		1.9.13
	
	
		org.springframework.boot
		spring-boot-starter-data-redis
	
	
		org.springframework.boot
		spring-boot-starter-freemarker
		2.0.3.RELEASE
	
	
		redis.clients
		jedis
		2.9.0
	




	
		
			org.springframework.boot
			spring-boot-maven-plugin
		
	

` springboot目录结构如下 ![在这里插入图片描述](https://img-blog.csdn.net/20181002093827398?watermark/2/text/aHR0cHM6Ly9ibG9nLmNzZG4ubmV0L2NzZG5fbXlwcm9ncmFtTGlmZQ==/font/5a6L5L2T/fontsize/400/fill/I0JBQkFCMA==/dissolve/70) application.properties配置文件如下:
#基本配置
server.port=8080
server.servlet.context-path=/springBoot
#spring.mvc.view.prefix=classpath:/templates/
#spring.mvc.view.suffix=.jsp
#spring.freemarker.prefix=classpath:/templates/
spring.freemarker.suffix=.html
spring.freemarker.request-context-attribute=rc
spring.freemarker.expose-request-attributes=true
spring.freemarker.settings.auto_import=common/include.html as common

#MySQL
spring.datasource.driver-class-name=com.mysql.jdbc.Driver
spring.datasource.url=jdbc:mysql://localhost:3306/mytest?characterEncoding=utf8
spring.datasource.username=root
spring.datasource.password=root
#jpa
spring.jpa.show-sql=true
spring.jpa.hibernate.ddl-auto=update
jpa.hibernate.dialect = org.hibernate.dialect.MySQL5Dialect

jpa如下:src->main->java->包名

@Repository
public interface StudengRepository extends JpaRepository {
    Student findStudentByAge(Integer age);
}

service :

public interface StudentService {
    ListgetList();
    void add(Student student);
    Student findByAge(Integer age);
    void delete(Student student);

    List findAll();
}

service实现类:

@Service
public class StudentServiceImpl implements StudentService {
    @Autowired
    private StudengRepository studengRepository;
    @Override
    public List getList() {
        return studengRepository.findAll();
    }

    @Override
    public void add(Student student) {
        Student student1=new Student();
        student1.setAge(26);
        student1.setName("li");
        student1.setScore(100);
        student1.setSex("女");
        studengRepository.save(student1);
    }

    @Override
    public Student findByAge(Integer age) {
        return studengRepository.findStudentByAge(age);
    }

    @Override
    public void delete(Student student) {
        studengRepository.delete(student);
    }

    @Override
    public List findAll() {
        return studengRepository.findAll();
    }
}

controller如下:

@Controller
public class StudentController {
    @Autowired
   private StudentService studentService;

/*    @GetMapping(value = "/getList")
    @ResponseBody
    public List getList(){
    List students=studentService.getList();
       return students;
    }*/
    @RequestMapping(value = "/list")
    public ModelAndView show(Model model, ModelAndView modelAndView) {
        modelAndView.setViewName("student_list");
        List students = studentService.findAll();
        model.addAttribute("student", students);
        return modelAndView;

    }
 

    @GetMapping(value = "/delete/{id}")
    @ResponseBody
    public void delete(Student student){
        studentService.delete(student);
    }

freemarker如下:list页面如下


学生管理

学生列表

<#list student as ct>
学生编号 学生名称 学生年龄 学生性别 学生成绩 操作
${ct.id} ${ct.name} ${ct.age} ${ct.sex} ${ct.score} 详情 添加 修改 删除 修改
<#-- --> <#-- -->

你可能感兴趣的:(java)