由于赶时间,明天上部署环境的图
跟spring一样,先创建一个Account实体类,
package com.kt.domain;
import java.io.Serializable;
public class Account implements Serializable{
private Integer id;
private String name;
private Double menoy;
public Integer getId() {
return id;
}
public void setId(Integer id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public Double getMenoy() {
return menoy;
}
public void setMenoy(Double menoy) {
this.menoy = menoy;
}
@Override
public String toString() {
return "Account{" +
"id=" + id +
", name='" + name + '\'' +
", menoy=" + menoy +
'}';
}
}
创建Dao层的AccountDao接口,创建Service层的AccountService接口
package com.kt.dao;
import com.kt.domain.Account;
import org.apache.ibatis.annotations.Select;
import java.util.List;
public interface AccountDao {
@Select("select * from account")
List<Account> findAll();
@Select("select * from account where id = ?")
Account findById(Integer id);
}
package com.kt.service;
import com.kt.domain.Account;
import java.util.List;
public interface AccountService {
List<Account> findAll();
void findById(Integer id);
}
创建service层的实现类,并引用Dao层的方法
package com.kt.service.Impl;
import com.kt.dao.AccountDao;
import com.kt.domain.Account;
import com.kt.service.AccountService;
import java.util.List;
public class AccountServiceImpl implements AccountService {
private AccountDao accountDao;
@Override
public List<Account> findAll() {
return accountDao.findAll();
}
@Override
public void findById(Integer id) {
accountDao.findById(1);
}
}
编写controller下的类,并写上各个注解
package com.kt.controller;
import com.kt.domain.Account;
import com.kt.service.AccountService;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import java.util.List;
@Controller
public class KtController {
private AccountService accountService;
@RequestMapping("/test")
public String hello(){
List<Account> all = accountService.findAll();
for (Account account : all) {
System.out.println(account);
}
return "hello";
}
@RequestMapping("/findId")
public String findId(){
Account account = new Account();
account.setName("你好啊");
account.setMenoy(1000.0);
accountService.findById(1);
return "redirect:/test";
}
}
删除原有的application.properties文件,创建application.yml文件(在原有的路径)
server:
servlet:
context-path: /springboot01
spring:
mvc:
view:
prefix: /WEB-INF/pages/
suffix: .jsp
datasource:
type: com.alibaba.druid.pool.DruidDataSource
driver-class-name: com.mysql.jdbc.Driver
url: jdbc:mysql://192.168.232.129:3306/account
data-username: root
data-password: 123456
在IDEA右上角点击资源配置,再进Modules里增加web文件,注意:修改路径..src\main\webapp..,版本选用2.5的
在webapp里创建要跳转的那些jsp文件
pom.xml中的那些依赖:
<?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 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>com.kt</groupId>
<artifactId>springboot01</artifactId>
<version>0.0.1-SNAPSHOT</version>
<packaging>jar</packaging>
<name>springboot01</name>
<description>Demo project for Spring Boot</description>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.1.0.RELEASE</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
<java.version>1.8</java.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>1.3.2</version>
</dependency>
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>druid</artifactId>
<version>1.0.9</version>
</dependency>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<scope>runtime</scope>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.apache.tomcat.embed</groupId>
<artifactId>tomcat-embed-jasper</artifactId>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
</project>
如果要增加事务,直接在service中的实现类上面添加一个@Transactional注解就可以了。