(一)MyBatis工程搭建
(删除)drop database if exists mybatis_demo;
(创建)create database mybatis_demo;
(使用)use mybatis_demo;
create table user (
id int auto_increment primary key,
username varchar(20),
age int,
score int
);
insert into user (id, username, age, score) values
(1,‘peter’, 18, 100), (2,‘pedro’, 24, 200),
(3,‘jerry’, 28, 500), (4,‘mike’, 12, 300),
(5,‘tom’, 27, 1000);
连接数据库导入MySQL包
(执行结果)username:peter
age:18
(二)引入MyBatis依赖
@SuppressWarnings({“SqlResolve”, “SqlNoDataSourceInspection”, “Duplicates”})
public class StartNoXml {
public static void main(String[] args) throws SQLException {
JdbcTransactionFactory jdbcTransactionFactory = new JdbcTransactionFactory();
PooledDataSource dataSource = new PooledDataSource(
“com.mysql.cj.jdbc.Driver”,
“jdbc:mysql://localhost:3306/mybatis_demo?useSSL=false”,
“root”,
“root”);
Environment environment = new Environment.Builder(“development”)
.transactionFactory(jdbcTransactionFactory)
.dataSource(dataSource).build();
Configuration configuration = new Configuration(environment);
SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(configuration);
SqlSession session = sqlSessionFactory.openSession();
PreparedStatement preStatement = session
.getConnection()
.prepareStatement(“SELECT * FROM user WHERE id = ?”);
preStatement.setInt(1, 1);
ResultSet result = preStatement.executeQuery();
while (result.next()) {
System.out.println("username: " + result.getString(“username”));
System.out.println("age: " + result.getString(“age”));
}
session.close();
}
}
(执行结果)username:peter
age:18
(三)for forEach 循环及Lambda表达式使用
for循环语句是循环语句,对比其他的的循环语句更简单
格式:for(初始化 判别式 更新) {
语句
}
forEach循环
for(元素类型 元素变量 对象){
代码语句
}
forEach循环 + Lambda表达式
Lambda表达式是java8的新特征之一,让我们可以更加方便地操作集合
forEach循环整合Lambda表达式后代码结构如下:
遍历对象.forEach(元素变量 -> 代码语句);
(四)SpringMVC工程搭建
<dependencies> <dependency> <groupId>junit</groupId> <artifactId>junit</artifactId> <version>4.13.2</version> <scope>test</scope> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-webmvc</artifactId> <version>5.2.13.RELEASE</version> </dependency> <dependency> <groupId>javax.servlet</groupId> <artifactId>servlet-api</artifactId> <version>2.5</version> </dependency> <dependency> <groupId>javax.servlet</groupId> <artifactId>javax.servlet-api</artifactId> <version>4.0.1</version> <scope>provided</scope> </dependency> </dependencies>
<build> <resources> <resource> <directory>src/main/java</directory> <includes> <include>**/*.properties</include> <include>**/*.xml</include> </includes> <filtering>false</filtering> </resource> <resource> <directory>src/main/resources</directory> <includes> <include>**/*.properties</include> <include>**/*.xml</include> </includes> <filtering>false</filtering> </resource> </resources> </build>
<?xml version="1.0" encoding="UTF-8"?> <beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:context="http://www.springframework.org/schema/context" xmlns:mvc="http://www.springframework.org/schema/mvc" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/context https://www.springframework.org/schema/context/spring-context.xsd http://www.springframework.org/schema/mvc https://www.springframework.org/schema/mvc/spring-mvc.xsd "> <!-- bean definitions here --> </beans>
(五)SpringMVC请求映射注解
@RequestMapping | 通用的请求处理 | 类 |
@GetMapping | 处理 HTTP GET 请求 | 查询方法 |
@PostMapping | 处理 HTTP POST 请求 | 新增修改方法 |
@PutMapping | 处理 HTTP PUT 请求 | 新增修改方法 |
@PatchMapping | 处理 HTTP PATCH 请求 | 新增修改方法 |
@DeleteMapping | 处理 HTTP DELETE 请求 | 删除方法 |