想对比与ssm框架 spring-boot也是基于ssm框架开发的 相比于ssm springboot书写起来更简单方便 ssm繁多的xml文件 在springboot中都可以被优化掉 通过大量的便签来实现多层结构之间的关联代码更简单易懂
创建一个maven工程
?导入jar包
<?xml version="1.0" encoding="UTF-8"?>
<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 https://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>com.njbdqn</groupId>
<artifactId>mysqb2</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>mysqb2</name>
<description>Demo project for Spring Boot</description>
<properties>
<java.version>1.8</java.version>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
<spring-boot.version>2.3.7.RELEASE</spring-boot.version>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.mybatis.spring.boot</groupId>
<artifactId>mybatis-spring-boot-starter</artifactId>
<version>2.1.4</version>
</dependency>
<dependency>
<groupId>com.oracle.database.jdbc</groupId>
<artifactId>ojdbc8</artifactId>
<scope>runtime</scope>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
<exclusions>
<exclusion>
<groupId>org.junit.vintage</groupId>
<artifactId>junit-vintage-engine</artifactId>
</exclusion>
</exclusions>
</dependency>
</dependencies>
<dependencyManagement>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-dependencies</artifactId>
<version>${spring-boot.version}</version>
<type>pom</type>
<scope>import</scope>
</dependency>
</dependencies>
</dependencyManagement>
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.8.1</version>
<configuration>
<source>1.8</source>
<target>1.8</target>
<encoding>UTF-8</encoding>
</configuration>
</plugin>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
<version>2.3.7.RELEASE</version>
<configuration>
<mainClass>com.njbdqn.mysqb2.Mysqb2Application</mainClass>
</configuration>
<executions>
<execution>
<id>repackage</id>
<goals>
<goal>repackage</goal>
</goals>
</execution>
</executions>
</plugin>
</plugins>
</build>
</project>
书写实体类
public class Bank {
private int id;
private String name;
private int 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;
}
}
定义方法接口
public interface BankDAO {
public List<Bank> findAll();
public void insertBank(Bank bank);
public void deletBank(int id);
public void updateBank(Bank bank);
}
控制层
@RestController
public class BankCtrl {
@Resource
private BankService bankService;
@RequestMapping("/all")
public List<Bank> find(){
return bankService.fall();
}
@RequestMapping("/insert")
public String insert(@RequestBody Bank bank){
bankService.insert(bank);
return "ok";
}
@RequestMapping("/del")
public String del(@RequestBody int id){
bankService.del(id);
return "ok";
}
@RequestMapping("/update")
public String up(@RequestBody Bank bank){
bankService.update(bank);
return "ok";
}
}
服务层
@Service
@Transactional //开启自动事务
public class BankService {
@Resource
private BankDAO bdao;
public List<Bank> fall(){
return bdao.findAll();
}
public void insert(Bank bank){
bdao.insertBank(bank);
}
public void del(int id){
bdao.deletBank(id);
}
public void update(Bank bank){
bdao.updateBank(bank);
}
}
测试层
@SpringBootApplication
@MapperScan("com.baoluo.mysqb2.dao")
public class Mysqb2Application {
public static void main(String[] args) {
SpringApplication.run(Mysqb2Application.class, args);
}
}
bank.xml
<?xml version="1.0" encoding="utf-8" ?>
<!DOCTYPE mapper PUBLIC
"-/mybatis.org//DTD Mapper 3.0//EN"
"http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<mapper namespace="com.baoluo.mysqb2.dao.BankDAO">
<select id="findAll" resultType="bank">
select * from bank
</select>
<insert id="insertBank" parameterType="bank">
insert into bank values (#{id},#{name},#{money})
</insert>
<delete id="deletBank" parameterType="bank">
delete from bank where id=#{id}
</delete>
<update id="updateBank" parameterType="bank">
update bank
<set>
<if test="name!=null">name=#{name},</if>
<if test="money!=0">name=#{money}</if>
</set>
<where>
<if test="id !=0">
id=#{id}
</if>
</where>
</update>
</mapper>
application.yml
spring:
datasource:
driver-class-name: oracle.jdbc.driver.OracleDriver
url: jdbc:oracle:thin:@192.168.80.171:1521:orcl
username: cm
password: ok
mybatis:
mapper-locations: classpath*:mapper/*.xml
type-aliases-package: com.njbdqn.mysqb2.entity
server:
port: 8081
然后启动程序
?spring-boot自带tomcat也不需要配置
然后打开网页
输入http://localhost:8081/all
?就可以看到数据库数据了 是不是比ssm简单多了
但是ssm还是根本? 框架会变但是ssm底层架构不会变
|