SpringBoot下搭建SSM框架
项目总体框架图 QQ截图20191009003619.png 一、创建一个新的工程 选择默认 —> next (基本点next就可以) QQ截图20191008232841.png建议改一下项目名 QQ截图20191008233741.png
勾选自己所需的功能 QQ截图20191008233851.png 其它的就点next就行了 IDEA自动生成项目后,在pom.xml文件中的 dependencies下会有如下几项 QQ截图20191009000155.png
接下来就是上代码
1、 在实体包 domain 下创建一个实体 Student
public class Student {
private int id;
private String name;
private int money;
public Student() {
}
public Student(String name, int money) {
this.name = name;
this.money = money;
}
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 getMoney() {
return money;
}
public void setMoney(int money) {
this.money = money;
}
@Override
public String toString() {
return "Student{" +
"id=" + id +
", name='" + name + '\'' +
", money=" + money +
'}';
}
}
2、在包 dao 下建一个接口 IStudentDao
import com.example.springboot_ssm.domain.Student;
import org.apache.ibatis.annotations.*;
import org.springframework.stereotype.Repository;
import java.util.List;
@Mapper
@Repository
public interface IStudentDao {
//增
@Insert("Insert into students(name,money) values(#{name},#{money})")
void add(Student student);
//删
@Delete("DELETE FROM students WHERE id=#{id}")
void delete(int id);
//改
@Update("UPDATE students SET name=#{name} , money=#{money} WHERE id=#{id}")
void update(Student student);
//查
@Select("SELECT * FROM students where id =#{id}")
Student query(int id);
@Select("SELECT * FROM students")
List<Student> queryall();
}
3、在包 service下建一个 StudentService 接口 和它的实现类 StudentServiceImpl
StudentService 代码
import com.example.springboot_ssm.domain.Student;
import java.util.List;
public interface StudentService {
void add(Student student);
void delete(int id);
Student query(int id);
List<Student> queryall();
void update(Student student);
boolean transfer_accounts(int from,int to,int money);
boolean transfer_accounts2(int from,int to,int money);
}
StudentServiceImpl 代码
import com.example.springboot_ssm.dao.IStudentDao;
import com.example.springboot_ssm.domain.Student;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import java.util.List;
@Service
@Transactional //申明要用事务 也可以写在方法上
public class StudentServiceImpl implements StudentService {
@Autowired
private IStudentDao iStudentDao;
@Override
public void add(Student student) {
iStudentDao.add(student);
}
@Override
public void delete(int id) {
iStudentDao.delete(id);
}
@Override
public Student query(int id) {
Student query = iStudentDao.query(id);
return query;
}
@Override
public List<Student> queryall() {
List<Student> queryall = iStudentDao.queryall();
return queryall;
}
@Override
public void update(Student student) {
iStudentDao.update(student);
}
/**
* 模拟正常转账
* @param from
* @param to
* @param money
* @return
*/
@Override
public boolean transfer_accounts(int from,int to,int money) {
Student query_from = iStudentDao.query(from);
Student query_to = iStudentDao.query(to);
query_from.setMoney(query_from.getMoney() - money);
query_to.setMoney(query_to.getMoney()+money);
iStudentDao.update(query_from);
iStudentDao.update(query_to);
return true;
}
/**
* 模拟异常转账
* @param from
* @param to
* @param money
* @return
*/
@Override
public boolean transfer_accounts2(int from, int to, int money) {
Student query_from = iStudentDao.query(from);
Student query_to = iStudentDao.query(to);
query_from.setMoney(query_from.getMoney() - money);
query_to.setMoney(query_to.getMoney()+money);
iStudentDao.update(query_to);
iStudentDao.update(query_from);
int i=1/0;
return true;
}
}
4、在包 controller下建一个 StudentController 类
import java.util.List;
@RestController
public class StudentController {
@Autowired
private StudentService studentService;
@RequestMapping("test")
public String test(){
return "测试成功";
}
@RequestMapping("add")
public String add(){
Student student = new Student();
student.setName("新添加");
student.setMoney(200);
studentService.add(student);
return "添加成功";
}
@RequestMapping("delete")
public String delete(int id){
studentService.delete(id);
return "删除成功";
}
@RequestMapping("query")
public Student query(int id){
Student query = studentService.query(id);
return query;
}
@RequestMapping("queryall")
public List<Student> queryall(){
List<Student> queryall = studentService.queryall();
return queryall;
}
@RequestMapping("update")
public String update(){
Student student = new Student();
student.setId(3);
student.setName("更改为3");
studentService.update(student);
return "更新成功";
}
@RequestMapping("t_a") //模拟正常
public boolean transfer_accounts(int from,int to,int money){
boolean b = studentService.transfer_accounts(from, to, money);
return b;
}
@RequestMapping("t_a2") //模拟异常
public boolean transfer_accounts2(int from,int to,int money){
boolean b = studentService.transfer_accounts2(from, to, money);
return b;
}
}
在启动类 SpringbootSsmApplication 开启事务管理 用 EnableTransactionManagement注解
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.transaction.annotation.EnableTransactionManagement;
@EnableTransactionManagement //开启事务管理
@SpringBootApplication
public class SpringbootSsmApplication {
public static void main(String[] args) {
SpringApplication.run(SpringbootSsmApplication.class, args);
}
}
代码就这么多了,最后就是配置信息,在application.properties下
#服务器端口号
server.port=80
#配置数据源
#我用的mysql是8.0以上,driver和url可能会和版本5的不一样,我觉得应该也可以用吧
spring.datasource.driver-class-name=com.mysql.cj.jdbc.Driver
spring.datasource.url=jdbc:mysql://localhost:3306/springboot_ssm?characterEncoding=utf-8 & serverTimezone=UTC & useSSL=false & allowPublicKeyRetrieval=TRUE
spring.datasource.username=root
spring.datasource.password=12345678
#mybatis映射
mybatis.type-aliases-package=com.example.springboot_ssm.domain
#配置日志 日志可要可不要
#显示执行与数据库相关操作的日志
logging.level.com.example.springboot_ssm.dao=debug
#日志文件路径
logging.path=/
#创建一个日志文件,名为stu.log,把日志存到文件中
logging.file=stu.log
最后启动项目就OK了
网友评论