用SpringBoot整合SSM+thymeleaf(很清晰,好方便啊)

  1. 目录结构
    用SpringBoot整合SSM+thymeleaf(很清晰,好方便啊)_第1张图片
    用SpringBoot整合SSM+thymeleaf(很清晰,好方便啊)_第2张图片
  2. pom.xml

	4.0.0
	com.bwf
	ssm
	0.0.1-SNAPSHOT
	
		org.springframework.boot
		spring-boot-starter-parent
		1.5.14.RELEASE
		
	

	
		
			org.springframework.boot
			spring-boot-starter-web
		
		
		
		
			org.springframework.boot
			spring-boot-starter-thymeleaf
			1.5.9.RELEASE
		
		
		
		
			org.springframework.boot
			spring-boot-devtools
			true
			true
		
		
		
		
		    mysql
		    mysql-connector-java
		    5.1.18
		
		
		
		
		    org.mybatis.spring.boot
		    mybatis-spring-boot-starter
		    1.3.2
		
	

  1. application.properties
server.port=9000
server.context-path=/ssm
#配thymeleaf模板
spring.thymeleaf.prefix=classpath:/templates/
spring.thymeleaf.suffix=.html
spring.thymeleaf.mode=HTML5
spring.thymeleaf.cache=false
spring.thymeleaf.encoding=UTF-8
spring.thymeleaf.content-type=text/html 
#整合mybatis时,先配数据源
spring.datasource.driverClassName =com.mysql.jdbc.Driver
spring.datasource.url =jdbc:mysql://localhost:3306/test?useUnicode=true&characterEncoding=utf-8
spring.datasource.username =root
spring.datasource.password =root1205
#mybatis支持
mybatis.mapper-locations=classpath:com/bwf/entity/*.xml
mybatis.type-aliases-package=com.entity 
logging.level.package=DEBUG
  1. 启动类
package com.bwf.main;

import org.mybatis.spring.annotation.MapperScan;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.annotation.ComponentScan;

@SpringBootApplication
@ComponentScan(basePackages={"com.bwf.controller","com.bwf.service"})
@MapperScan(basePackages={"com.bwf.dao"})
public class StartSSM {

	public static void main(String[] args) {
		SpringApplication.run(StartSSM.class);

	}

}

  1. PersonController.java
package com.bwf.controller;

import java.util.ArrayList;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;

import com.bwf.entity.Person;
import com.bwf.service.IPersonService;

@Controller
public class PersonController {
	
	@Autowired
	private IPersonService personService;
	
	@RequestMapping("m1")
	//测试一下
	public String m1(){
		return "index";
	}
	
	@RequestMapping("m2")
	@Transactional //以前配事务很麻烦,现在只用加此注解,一般在业务层加事务
	//查询不用加事务,增删改查两条数据以上要加事务
	//向数据库连续插入两条数据,事务操作
	public String m2(){
		System.out.println(personService);
		personService.reg("张梦", 23);
		//System.out.println(1/0);//用此异常测试事务
		personService.reg("党银银", 27);
		return "index";
	}
	@RequestMapping("m3")
	public String m3(ModelMap mm){
		//从数据库查询得到person表的一条数据
		Person p = personService.find(9);
		System.out.println(p.getName());
		return "index";
	}
	
	@RequestMapping("m4")
	@Transactional
	//向数据库批量插入
	public String m4(){
		Person p1 = new Person();
		p1.setName("王大锤");
		Person p2 = new Person();
		p2.setName("马可");
		Person p3 = new Person();
		p3.setName("古筝");
		List list = new ArrayList();
		list.add(p1);
		list.add(p2);
		list.add(p3);
		personService.add(list);
		return "index";
	}
	
	@RequestMapping("m5")
	public String m5(ModelMap mm){
		//从数据库取出整个person表,并将它显示在前台页面上
		//System.out.println(personService.selectAll().size());
		List list = personService.selectAll();
		mm.addAttribute("pList", list);
		//System.out.println(list);
		return "person_db";
	}
	
	@RequestMapping("m6")
	public String m6(){
		//修改数据库中的某条数据
		Person p = new Person();
		p.setId(1);
		p.setName("张君宝");
		p.setAge(149);
		personService.modify(p);
		return "person_db";
	}

}
  1. IPersonService和PersonService
package com.bwf.service;

import java.util.List;

import com.bwf.entity.Person;

public interface IPersonService {
	
	public void reg(String name,int age);
	
	public Person find(int id);
	
	public void add(List list);
	
	public List selectAll();
	
	public void modify(Person p);

}

package com.bwf.service;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.bwf.dao.PersonMapper;
import com.bwf.entity.Person;

@Service
public class PersonService implements IPersonService {
	
	@Autowired
	private PersonMapper personMapper;
	

	public void reg(String name, int age) {
		System.out.println(personMapper);
		//System.out.println("注册过了"+name+","+age);
		personMapper.insertPerson(name, age);
	}


	public Person find(int id) {
		return personMapper.selectPerson(id);
	}


	public void add(List list) {
		for(int i = 0; i < list.size();i++){
			personMapper.addPerson(list.get(i));
		}
		
	}


	public List selectAll() {
		return personMapper.selectAll();
	}


	public void modify(Person p) {
		personMapper.setPerson(p);
		
	}

}
  1. PersonMapper.java
package com.bwf.dao;

import java.util.List;

import org.apache.ibatis.annotations.Param;

import com.bwf.entity.Person;

public interface PersonMapper {
	public void insertPerson(@Param("name")String name,@Param("age")int age);
	
	public Person selectPerson(int id);
	
	public void addPerson(Person p);
	
	public List selectAll();
	
	public void setPerson(Person p);
}
  1. Person.xml



	
	INSERT INTO person(name,age)VALUES(#{name},#{age})
	
	
	
	INSERT INTO person(id,name,age,sex,pwd)VALUES(#{id},#{name},#{age},#{sex},#{pwd})
	
	
	
	UPDATE person SET name=#{name},age=#{age} WHERE id=1
	
	

  1. Person.java
package com.bwf.entity;

public class Person {
	
	private int id;
	private String name;
	private int age;
	private String sex;
	private String pwd;
	public int getId() {
		return id;
	}
	public void setId(int id) {
		this.id = id;
	}
	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 getSex() {
		return sex;
	}
	public void setSex(String sex) {
		this.sex = sex;
	}
	public String getPwd() {
		return pwd;
	}
	public void setPwd(String pwd) {
		this.pwd = pwd;
	}
}
  1. person_db.html




从数据库取出整张表


templates下的person_db.html
No. id name age sex pwd
  1. 以上是基本SSM框架的整合,Redis模板待整合,待补充

你可能感兴趣的:(SSM,SpringBoot)