一、Maven
概念: 是一种用于管理、构建
java
项目的工具
作用: 依赖配置,一键构建
1.1Maven的工作原理
1.2 安装maven
1.2.1 解压到当前文件夹下
1.2.2 配置maven
配置本地仓库路径: 安装目录
-->
conf--->
settings.xml
![](https://i-blog.csdnimg.cn/blog_migrate/f21f59bbcdc5b6b2da86e2b967653145.png)
![](https://i-blog.csdnimg.cn/blog_migrate/87c88f996e29b72b926bda6ee4a7968e.png)
<mirror>
<id>alimaven</id>
<name>aliyun maven</name>
<url>http://maven.aliyun.com/nexus/content/groups/public/</url>
<mirrorOf>central</mirrorOf>
</mirror>
注意: 一定要记得 ctrl+s
保存
1.3 验证maven有没有安装成功
1.4 IDEA中集成Maven
![](https://i-blog.csdnimg.cn/blog_migrate/751748fa7dc0d9deec0eebd5fcee102d.png)
1.5 创建Maven项目
1.6 maven的坐标
作用:唯一标识某一个
jar
包
组成:
groupId:
公司名或者组织名
artifactId:
项目名
version:
版本号
如果要在
pom
中指定需要使用的
jar
包
groupId, artifactId,version
使用
servlet
的
jar
包
1.7 maven项目的项目结构
1.8 maven项目的构建
maven
提供了非常多的指令用来操作一个项目不同阶段
maven
项目编译后的产物:
target
目录中
二、web程序快速入门
框架:软件的半成品
(
别人写好的
)
spring,springmvc,mybatis,springboot,springcloud....
学习:
spring,mybatis,springmvc,springboot
springboot....
快速入门的步骤:
1.创建一个maven项目![](https://i-blog.csdnimg.cn/blog_migrate/52b2bdb42be8d77762ace1121b649c3c.png)
2.让当前的maven项目变成springboot项目
2.1 当前项目继承boot项目![](https://i-blog.csdnimg.cn/blog_migrate/379ace2ffc2c7c236b040f0b00a74890.png)
2.2 引入当前项目需要的依赖![](https://i-blog.csdnimg.cn/blog_migrate/9ae3501c57ddf08a6141ae5535da2a03.png)
3.提供接收请求的类
以前
:
提供一个类
,
继承
Servlet,
重写
doGet
和
doPost
方法
现在
:
提供一个类
,
在类上添加一个注解@RestController, 在类中提供对应处理请求的方法
![](https://i-blog.csdnimg.cn/blog_migrate/b0511ca7bed2e8d23f1c07362a2aadf4.png)
4.提供boot程序的启动类(固定书写)
注意: 启动类必须在其他类的同包或者父包下
![](https://i-blog.csdnimg.cn/blog_migrate/bb5585e800ffc14f5f478821a954e020.png)
5. 测试
运行启动类的主方法
三、HTTP协议
协议:制定规则
http
协议: hyper text translate protocle: 超文本传输协议,规定数据发送方和数据接收房使用的数据格式
请求消息格式:
四、请求消息的解析以及响应消息的封装
![](https://i-blog.csdnimg.cn/blog_migrate/0ce62c0460678d8d567548dd3d2e9cc1.png)
格式:
请求消息格式:
响应消息格式: ![](https://i-blog.csdnimg.cn/blog_migrate/f57a34f258ac3fa074b86644367a64fe.png)
四、请求消息的解析以及响应消息的封装![](https://i-blog.csdnimg.cn/blog_migrate/9620be68442109a37b83da46779dba4e.png)
五、如何接收请求消息
1.键值对消息
概念:
username=
张三
&password=123&gender=
男
直接在方法的参数列表上声明对应的参数变量即可,要求:
方法参数的名字需要和请求参数的名字保持一致
如果方法的参数名和请求参数名字不一致
,
需要通过
RequestParam
注解来解决
![](https://i-blog.csdnimg.cn/blog_migrate/2a7f418b3b3a33a55d579dbadf9e9b26.png)
2.模拟http请求发送的工具
http
协议是一种公共的协议
,
只需要满足
http
的要求
,
任何工具都可以发送
http
请求
,
使用浏览器可以发送
, PostMan...
PostMan
使用
:
1.安装postman: 眼睛闭住,一路下一步
2.注册postman
3.创建一个链接
4.填写内容完成测试
3.json格式的数据
概念
: javascript object notation: js
对象的表现形式
客户端传递参数:
//js中{}代表对象
//js中[]带代表数组
var student = {name:"石油姐",age:23,gender:"女"}
/* alert(student)
alert(student.name)
alert(student.age)
alert(student.gender)*/
/*var students=[{name:"石油姐",age:23,gender:"女"},{name:"龚玥
菲",age:23,gender:"女"},{name:"凤姐",age:23,gender:"女"}];
alert(students)
alert(students[1].name)*/
//js对象中,对象可以嵌套数组,数组也可以嵌套对象
var classroom = {num:28,students:[{name:"石油姐",age:23,gender:"女"},{name:"龚玥
菲",age:23,gender:"女"},{name:"凤姐",age:23,gender:"女"}]}
alert(classroom.students[1].name)
json
字符串
:
程序员发现
js
对象存储数据的格式非常的简单明了
,
广大程序员就把
js
对象的表现形式作为浏览器和服务
器之间传递数据的格式来使用
,
出现了
json
字符串
注意
:
键和值必须用双引号引起来
//js中{}代表对象
//js中[]带代表数组
var student = {name:"石油姐",age:23,gender:"女"}
/* alert(student)
alert(student.name)
alert(student.age)
alert(student.gender)*/
/*var students=[{name:"石油姐",age:23,gender:"女"},{name:"龚玥
菲",age:23,gender:"女"},{name:"凤姐",age:23,gender:"女"}];
alert(students)
alert(students[1].name)*/
//js对象中,对象可以嵌套数组,数组也可以嵌套对象
var classroom = {num:28,students:[{name:"石油姐",age:23,gender:"女"},{name:"龚玥
菲",age:23,gender:"女"},{name:"凤姐",age:23,gender:"女"}]}
alert(classroom.students[1].name)
//json字符串在任何语言里面都可以使用,不局限于javascript
var studentStr = '{"name":"石油姐","age":"23","gender":"女"}';
alert(studentStr)
服务器和浏览器之间传递
json
字符串
:
public class User {
private String name;
private Integer age;
private String gender;
//空参数构造: 在类中的空白处 右击---->generate----->constructor---->select none
public User() {
}
//全参数构造: 在类中的空白处 右击---->generate----->constructor----ctrl+A 选中全部的成员变
量---->ok
public User(String name, Integer age, String gender) {
this.name = name;
this.age = age;
this.gender = gender;
}
//setter/getter方法: 在类中的空白处 右击---->generate----->setter and getter ----
ctrl+A 选中全部的成员变量---->ok
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public Integer getAge() {
return age;
}
public void setAge(Integer age) {
this.age = age;
}
public String getGender() {
return gender;
}
public void setGender(String gender) {
this.gender = gender;
}
}
public class Test {
public static void main(String[] args) {
String userStr = "{\"name\":\"石油姐\",\"age\":\"23\",\"gender\":\"女\"}";
//获取User对象的name属性值
User user = new User("石油姐",23,"女");
user.getName();
}
}
客户端传递json字符串:
完成后台代码:
3.1 练习
- 1.在pojo包下,创建User类
- 2.在PostMan中编写好测试的请求
- 3.在UserController类中添加addUser方法
4.数组格式的消息获取
客户端传递参数:
后台代码的编写: ![](https://i-blog.csdnimg.cn/blog_migrate/ad2f64fe3f4688a400ff1035132ca58a.png)
六、 如何响应数据
1.响应字符串
如果方法的返回值类型是字符串
,
那么原封不动的响应回去
2.响应对象
springboot
会自动的把返回的
java
对象转换成
json
格式字符串响应给浏览器
![](https://i-blog.csdnimg.cn/blog_migrate/b9c27b640f0cd6b15f42b422a9b2750f.png)
![](https://i-blog.csdnimg.cn/blog_migrate/9cba982671c35f6262b0a1628d83dc75.png)
3.统一响应结果
前端和后端需要经过协商
,
统一响应结果的格式
类型
:json
字符串
组成
: {code:"
响应码
", msg:"
提示信息
",data:"
响应的结果数据
"}
code: 0 -
代表本次请求操作失败
msg: "
用户名或者密码错误
"
data: {} [] ""
3.1 定义一个类,必须有三个成员变量: code,msg,data
public class Result {
private Integer code;//响应码 0 --失败 1---成功
private String msg;//提示信息
private Object data;//响应数据
public Result() {
}
public Result(Integer code, String msg, Object data) {
this.code = code;
this.msg = msg;
this.data = data;
}
//定义一个静态方法,用来快速的生成响应成功的Result对象
public static Result success(Object data){
return new Result(1,"操作成功",data);
}
//定义一个静态方法,用来快速的生成响应失败的Reuslt对象
public static Result error(String msg){
return new Result(0,msg,null);
}
public Integer getCode() {
return code;
}
public void setCode(Integer code) {
this.code = code;
}
public String getMsg() {
return msg;
}
public void setMsg(String msg) {
this.msg = msg;
}
public Object getData() {
return data;
}
public void setData(Object data) {
this.data = data;
}
}
3.2 借助于Result,响应统一的结果数据
@RequestMapping("/getAllUsers2")
public Result getAllUsers2(){
//准备集合,存放多个user,并返回
ArrayList<User> users = new ArrayList<>();
users.add(new User("龚玥菲",23,"女"));
users.add(new User("柳岩",18,"女"));
users.add(new User("李师师",18,"女"));
//构造一个Result对象
//Result result = new Result(1,"操作成功",users);
Result result = Result.success(users);
return result;
}
练习:
1.
在
pojo
包下
,
定义
Result
类
2.
在
UserController
类中
,
提供
getAllUsers2
方法
七、Spring的IOC
思考: java中,所有的方法都需通过对象来调用。。。在整个程序中,都没有写new UserControlller()创建对象,但 是里面的方法正常调用了, 问题: 对象在哪??
IOC
概念:
inverse of control:
控制反转。把对象的创建权进行反转
:
以前使用对象
,
需要我们手动的
new
创建对象
,
使用了 Spring后
,
我们可以把对象的创建权交给
Spring
交给Spring
的方式
:
在类上添加了
:
在
UserController
类上添加了
@RestController
注解
控制反转的作用:
![](https://i-blog.csdnimg.cn/blog_migrate/f424007148c625fd8aba9832faf1346d.png)
获取
IOC
容器
:
public static void main(String[] args) {
//启动一个spring的程序
//run方法执行完毕后,会返回Spring的IOC容器
ApplicationContext context = SpringApplication.run(App.class,args);
System.out.println(context);
}
获取
IOC
容器中的对象 :
@SpringBootApplication//标识当前类是一个启动类
public class App {
public static void main(String[] args) {
//启动一个spring的程序
//run方法执行完毕后,会返回Spring的IOC容器
ApplicationContext context = SpringApplication.run(App.class,args);
System.out.println(context);
//getBean(String name); 根据键获取值
Object obj = context.getBean("userController");
System.out.println(obj);
//getBean(类名.class); 根据类型获取值
UserController obj2 = context.getBean(UserController.class);
System.out.println(obj2);
}
}
还有哪些方式可以把某个对象的创建权交给
Spring:
在类上添加如下注解
-
@Component@Controller@Service@Repostory....
八、SpringBoot配置文件的使用
前提:
Spring
会给程序做很多的默认配置
设置: 如果
SPring
提供的默认配置不能满足我们的要求,那么可以在
SpringBoot
的配置文件中进行修改
配置文件位置:
main/resources
配置文件的名字:
application.properties
使用:
![](https://i-blog.csdnimg.cn/blog_migrate/0804bc5adcef6bfe6871cdfccea85800.png)
![](https://i-blog.csdnimg.cn/blog_migrate/a9d1716ac6a839423b16d847b0a029e8.png)
![](https://i-blog.csdnimg.cn/blog_migrate/d8bca84d9f462d47e4736e71aeb2777f.png)
九、数据库
分类:
非关系型数据库: 存储的数据与数据之间没有任何关系 redis hbase mongoDb
关系型数据库:存储的数据与数据之间有关系
MySQL sqlserver DB2 Oracle SyqlLite....
安装:
参照文档安装即可
验证安装成功:
mysql -u
用户名
-p
密码
![](https://i-blog.csdnimg.cn/blog_migrate/d2c5f2842bd5b25f05258c72d251488c.png)
关系型数据库的操作:
SQL: structure query language:
结构化查询语言, 定义了所有关系型数据库的操作规则
1.SQL语言分类
DDL: 数据库的增删改查,以及数据表的增删改查
DML: 对表中数据的增删改
DQL: 对表中数据的查询
DCL: 有关权限的操作
2.DDL
数据库的操作:
查询库:
show databases;
创建库:
create database
数据库名
;
create database db1;
删除库:
drop database
数据库名
;
drop database db1;
使用库:
use
数据库名
;
数据表的操作
:
drop table
表名
3.DML
添加数据
insert into
表名
(
字段
1,
字段
2,
字段
3...) values(
值
1,
值
2,
值
3....)
修改数据
update
表名
set
字段
1=
值
1,
字段
2=
值
2... --
会修改表中所有的数据
update
表名
set
字段
1=
值
1,
字段
2=
值
2... where
条件
删除数据
delete from
表名
--
会删除表中所有的数据
delete from
表名
where
条件
4.DQL
基本查询语句的格式
:
查询所有的数据
select
字段
1,
字段
2....from
表名
select * from
表名
条件查询
select
字段
1,
字段
2....from
表名
[where
条件
[]
条件
: > >= < <= = and or between...and...
排序查询
select
字段
1,
字段
2....from
表名
where
条件
order by
排序的字段 排序的方式
(ASC|DESC)
分页查询
:
select
字段
1,
字段
2....from
表名
limit
开始查询的索引
,
每页显示的条数
# 页码和起始索引的关系 起始索引 = (页码-1)*每页条数
# 1---- (1-1)*3 = 0
# 2---- (2-1)*3 = 3
# 3---- (3-1)*3 = 6
# 创建表
create table student(
name varchar(32),
age int,
gender char(1),
score double(5,2)
);
show tables;
drop table student;
#添加数据
insert into student(name,age,gender,score) values('如花',23,'男',88.88);
insert into student(name,age,gender,score) values('如花他妈',33,'女',99.88);
insert into student(name,age,gender,score) values('如花他哥',25,'男',33.33);
insert into student(name,age,gender,score) values('如花他弟',12,'男',66.66);
insert into student(name,age,gender,score) values('如花他爸',38,'男',55.55);
insert into student(name,age,gender,score) values('柳岩',42,'女',99.99);
insert into student(name,age,gender,score) values('吴亦凡',24,'男',0.00);
#修改数据
update student set age=88;
update student set age = 11 where name='如花';
update student set gender='女',score=22.22 where name='如花';
#查询表中所有数据的sql
select * from student;
#删除表中的数据
delete from student;
delete from student where name='如花他妈';
#查询sql
select name,gender from student;
select name,age,gender from student;
select name,age,gender,score from student;
select * from student;
#条件查询
#查询成绩大于60分的学生
select * from student where score>60;
#查询成绩等于99.88分的学生
select * from student where score = 99.88;
#成绩在 10~40之间的学生
select * from student where score>=10 and score<=40;
select * from student where score between 10 and 40;
#成绩是 22.22 或者成绩是99.88
select * from student where score=22.22 or score=99.88;
select * from student where score in(22.22,99.88);
#查询所有成绩大于33.33的学生,并且按照成绩降序查询
select * from student where score>33.33 order by score DESC;
select * from student where score>33.33 order by score ASC;
# 查询第一页的数据 每页3条
#select 字段1,字段2....from 表名 limit 开始查询的索引,每页显示的条数
select * from student limit 0,3;
# 查询第二页的数据 每页3条
select * from student limit 3,3;
# 查询第三页的数据 每页3条
select * from student limit 6,3;
# 页码和起始索引的关系 起始索引 = (页码-1)*每页条数
# 1---- (1-1)*3 = 0
# 2---- (2-1)*3 = 3
# 3---- (3-1)*3 = 6
练习 :
创建一张员工表,
员工表的字段
:
姓名 年龄 工资 职位 入职日期
# 创建数据库 db2
create database db2;
# 使用数据库 db2
use db2;
# 创建一张员工表,员工表的字段: id 姓名 年龄 工资 职位 入职日期
create table emp(
id int primary key auto_increment,
name varchar(32),
age int,
salary int,
job varchar(10),
entrydate date
);
#数据添加: 一般添加的时候不给id添加数据, id数据库会自动添加
insert into emp(name,age,salary,job,entrydate) values ('张三',23,6000,'教学主管','2000-
01-01');
insert into emp(name,age,salary,job,entrydate) values ('张三1',22,6000,'教学主管','2000-
01-01');
insert into emp(name,age,salary,job,entrydate) values ('张三2',28,6000,'教学主管','2000-
01-01');
insert into emp(name,age,salary,job,entrydate) values ('张三3',21,6000,'教学主管','2000-
03-01');
insert into emp(name,age,salary,job,entrydate) values ('张三4',23,6000,'教学主管','2000-
01-01');
insert into emp(name,age,salary,job,entrydate) values ('张三5',25,6000,'教学主管','2000-
05-01');
insert into emp(name,age,salary,job,entrydate) values ('张三6',30,6000,'教学主管','2000-
01-01');
#修改数据: 张三6的入职日期修改为 2001-01-01
update emp set entrydate='2001-01-01' where name='张三6';
#查询所有的员工信息
select * from emp;
#查询年龄大于25的员工信息
select * from emp where age>25;
#查询入职日期在2000-01-01~2000-03-01
select * from emp where entrydate between '2000-01-01' and '2000-03-01';
# 查询所有员工的第一页数据 每页条数3
select * from emp limit 0,3;
# 删除员工id为1,2,3的员工
delete from emp where id in(1,2,3);
5.IDEA的数据库插件使用
1.调出视图
2023版本要下载插件:Database Navigator 插件图标在左边
2.创建mysql的链接
3.设置链接
4.查看数据库
5.新建编写sql的面板
6.编写sql并且执行sql
十、MyBatis操作数据库
Mybatis: 一款用于操作关系型数据库的java
框架,它的底层是
jdbc
0.执行sql
# 创建数据库 db2
create database db2;
# 使用数据库 db2
use db2;
# 创建一张员工表,员工表的字段: id 姓名 年龄 工资 职位 入职日期
create table emp(
id int primary key auto_increment,
name varchar(32),
age int,
salary int,
job varchar(10),
entrydate date
);
#数据添加: 一般添加的时候不给id添加数据, id数据库会自动添加
insert into emp(name,age,salary,job,entrydate) values ('张三',23,6000,'教学主管','2000-
01-01');
insert into emp(name,age,salary,job,entrydate) values ('张三1',22,6000,'教学主管','2000-
01-01');
insert into emp(name,age,salary,job,entrydate) values ('张三2',28,6000,'教学主管','2000-
01-01');
insert into emp(name,age,salary,job,entrydate) values ('张三3',21,6000,'教学主管','2000-
03-01');
insert into emp(name,age,salary,job,entrydate) values ('张三4',23,6000,'教学主管','2000-
01-01');
insert into emp(name,age,salary,job,entrydate) values ('张三5',25,6000,'教学主管','2000-
05-01');
insert into emp(name,age,salary,job,entrydate) values ('张三6',30,6000,'教学主管','2000-
01-01');
#修改数据: 张三6的入职日期修改为 2001-01-01
update emp set entrydate='2001-01-01' where name='张三6';
#查询所有的员工信息
select * from emp;
#查询年龄大于25的员工信息
select * from emp where age>25;
#查询入职日期在2000-01-01~2000-03-01
select * from emp where entrydate between '2000-01-01' and '2000-03-01';
# 查询所有员工的第一页数据 每页条数3
select * from emp limit 0,3;
# 删除员工id为1,2,3的员工
delete from emp where id in(1,2,3);
1.快速入门
1.1 在pom.xml文件中, 引入Mybatis的坐标,引入Mysql的坐标
<?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>
<!--让当前项目继承springboot父工程-->
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.4.5</version>
</parent>
<groupId>com.itheima</groupId>
<artifactId>quickstart</artifactId>
<version>1.0-SNAPSHOT</version>
<properties>
<maven.compiler.source>17</maven.compiler.source>
<maven.compiler.target>17</maven.compiler.target>
</properties>
<!--引入web的依赖 内置一个tomcat, 随着项目的启动而启动-->
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
<version>2.4.5</version>
</dependency>
<!--mysql的坐标-->
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>8.0.22</version>
<!--如果你的mysql是5.x版本,那么使用下面这个依赖坐标-->
<!--<version>5.1.47</version>-->
</dependency>
<!--mybatis的坐标-->
<dependency>
<groupId>org.mybatis.spring.boot</groupId>
<artifactId>mybatis-spring-boot-starter</artifactId>
<version>2.2.2</version>
</dependency>
</dependencies>
</project>
1.2 在application.properties配置文件中配置数据库的信息
# 修改tomcat的端口号, 键名是固定的,值可以修改
# =前后不能有空格!!!!!!!!!!!!
# =前后不能有空格!!!!!!!!!!!!
# =前后不能有空格!!!!!!!!!!!!
server.port=9090
# 写很多其他的配置。。。。
# 配置数据库连接信息
# mysql5.x的驱动类
# spring.datasource.driver-class-name=com.mysql.jdbc.Driver
# mysql 8.x的驱动类
spring.datasource.driver-class-name=com.mysql.cj.jdbc.Driver
# 配置连接
spring.datasource.url=jdbc:mysql://localhost:3306/db2?serverTimezone=Asia/Shanghai
# 配置用户名
spring.datasource.username=root
# 用户名和密码要配置成你自己的
spring.datasource.password=1234
application.properties配置是yml语言,不是特有的标记语言。
1.3 在mapper包下,提供接口,接口上需要添加@Mapper注解(会把该接口的实现类对象加入到IOC容器中)
1.4 需要在接口中提供方法,并且在方法上通过注解编写sql
1.4.1 在pojo包下,提供java类Emp
public class Emp {
//成员变量: 成员变量的名字需要和字段名保持一致
private Integer id;
private String name;
private Integer age;
private Integer salary;
private String job;
private Date entrydate;
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 Integer getAge() {
return age;
}
public void setAge(Integer age) {
this.age = age;
}
public Integer getSalary() {
return salary;
}
public void setSalary(Integer salary) {
this.salary = salary;
}
public String getJob() {
return job;
}
public void setJob(String job) {
this.job = job;
}
public Date getEntrydate() {
return entrydate;
}
public void setEntrydate(Date entrydate) {
this.entrydate = entrydate;
}
@Override
public String toString() {
return "Emp{" +
"id=" + id +
", name='" + name + '\'' +
", age=" + age +
", salary=" + salary +
", job='" + job + '\'' +
", entrydate=" + entrydate +
'}';
}
}
1.4.2 在接口中添加方法
@Mapper//添加这个注解,将来mybatis会把EmpMapper接口的实现类对象放入到IOC容器中
public interface EmpMapper {
@Select("select * from emp")
public ArrayList<Emp> selectAll();
}
1.5 完成测试: 在主启动类中,获取容器中Mapper接口的实现类对象,调用方法完成操作
@SpringBootApplication//标识当前类是一个启动类
public class App {
public static void main(String[] args) {
//启动一个spring的程序
//run方法执行完毕后,会返回Spring的IOC容器
ApplicationContext context = SpringApplication.run(App.class,args);
/* System.out.println(context);
//getBean(String name); 根据键获取值
Object obj = context.getBean("userController");
System.out.println(obj);
//getBean(类名.class); 根据类型获取值
UserController obj2 = context.getBean(UserController.class);
System.out.println(obj2);
Animal a = context.getBean(Animal.class);
System.out.println(a);*/
//从容器中获取EmpMapper接口的实现类对象
EmpMapper empMapper = context.getBean(EmpMapper.class);
ArrayList<Emp> emps = empMapper.selectAll();
System.out.println(emps);
}
}
2.根据ID查询指定的员工
2.1 在EmpMapper接口中提供方法
@Mapper//添加这个注解,将来mybatis会把EmpMapper接口的实现类对象放入到IOC容器中 (实现类是mybatis自
动提供的,实现类的对象也是mybatis自动创建的, 创建好的对象会放入到IOC容器中)
public interface EmpMapper {
@Select("select * from emp")
public ArrayList<Emp> selectAll();
//完成根据ID查询
//如果要获取参数的值,拼接到sql中, #{参数名}
@Select("select * from emp where id=#{id}")
public Emp findById(Integer id);
}
2.2 在主启动类中完成测试
@SpringBootApplication//标识当前类是一个启动类
public class App {
public static void main(String[] args) {
//启动一个spring的程序
//run方法执行完毕后,会返回Spring的IOC容器
ApplicationContext context = SpringApplication.run(App.class,args);
/* System.out.println(context);
//getBean(String name); 根据键获取值
Object obj = context.getBean("userController");
System.out.println(obj);
//getBean(类名.class); 根据类型获取值
UserController obj2 = context.getBean(UserController.class);
System.out.println(obj2);
Animal a = context.getBean(Animal.class);
System.out.println(a);*/
//从容器中获取EmpMapper接口的实现类对象
EmpMapper empMapper = context.getBean(EmpMapper.class);
/*ArrayList<Emp> emps = empMapper.selectAll();
System.out.println(emps);*/
Emp emp = empMapper.findById(5);
System.out.println(emp);
}
}
3.完成数据的新增
3.1在EmpMapper接口中添加方法
@Mapper//添加这个注解,将来mybatis会把EmpMapper接口的实现类对象放入到IOC容器中 (实现类是mybatis自
动提供的,实现类的对象也是mybatis自动创建的, 创建好的对象会放入到IOC容器中)
public interface EmpMapper {
@Select("select * from emp")
public ArrayList<Emp> selectAll();
//完成根据ID查询
//如果要获取参数的值,拼接到sql中, #{参数名}
@Select("select * from emp where id=#{id}")
public Emp findById(Integer id);
//完成员工的新增
//如果要拿对象中的成员变量的值拼接到sql中, #{成员变量的名字}
@Insert("insert into emp(name,age,salary,job,entrydate) values(#{name},#{age},#
{salary},#{job},#{entrydate})")
public void addEmp(Emp emp);
}
3.2在主启动类中完成测试
public class App {
public static void main(String[] args) {
//启动一个spring的程序
//run方法执行完毕后,会返回Spring的IOC容器
ApplicationContext context = SpringApplication.run(App.class,args);
/* System.out.println(context);
//getBean(String name); 根据键获取值
Object obj = context.getBean("userController");
System.out.println(obj);
//getBean(类名.class); 根据类型获取值
UserController obj2 = context.getBean(UserController.class);
System.out.println(obj2);
Animal a = context.getBean(Animal.class);
System.out.println(a);*/
//从容器中获取EmpMapper接口的实现类对象
EmpMapper empMapper = context.getBean(EmpMapper.class);
/*ArrayList<Emp> emps = empMapper.selectAll();
System.out.println(emps);*/
/* Emp emp = empMapper.findById(5);
System.out.println(emp);*/
Emp emp = new Emp();
emp.setName("王力宏");
emp.setAge(48);
emp.setSalary(1000000);
emp.setJob("所长");
emp.setEntrydate(new Date());
empMapper.addEmp(emp);
}
}
4.完成员工的修改
4.1 在EmpMapper接口中提供方法
@Mapper//添加这个注解,将来mybatis会把EmpMapper接口的实现类对象放入到IOC容器中 (实现类是mybatis自
动提供的,实现类的对象也是mybatis自动创建的, 创建好的对象会放入到IOC容器中)
public interface EmpMapper {
@Select("select * from emp")
public ArrayList<Emp> selectAll();
//完成根据ID查询
//如果要获取参数的值,拼接到sql中, #{参数名}
@Select("select * from emp where id=#{id}")
public Emp findById(Integer id);
//完成员工的新增
//如果要拿对象中的成员变量的值拼接到sql中, #{成员变量的名字}
@Insert("insert into emp(name,age,salary,job,entrydate) values(#{name},#{age},#
{salary},#{job},#{entrydate})")
public void addEmp(Emp emp);
//完成员工的修改
//如果要拿对象中的成员变量的值拼接到sql中, #{成员变量的名字}
@Update("update emp set name=#{name},age=#{age},salary=#{salary},job=#
{job},entrydate=#{entrydate} where id=#{id}")
public void updateEmp(Emp emp);
}
4.2 在住启动类App中完成测试
@SpringBootApplication//标识当前类是一个启动类
public class App {
public static void main(String[] args) {
//启动一个spring的程序
//run方法执行完毕后,会返回Spring的IOC容器
ApplicationContext context = SpringApplication.run(App.class,args);
/* System.out.println(context);
//getBean(String name); 根据键获取值
Object obj = context.getBean("userController");
System.out.println(obj);
//getBean(类名.class); 根据类型获取值
UserController obj2 = context.getBean(UserController.class);
System.out.println(obj2);
Animal a = context.getBean(Animal.class);
System.out.println(a);*/
/*ArrayList<Emp> emps = empMapper.selectAll();
System.out.println(emps);*/
/* Emp emp = empMapper.findById(5);
System.out.println(emp);*/
/*Emp emp = new Emp();
emp.setName("王力宏");
emp.setAge(48);
emp.setSalary(1000000);
emp.setJob("所长");
emp.setEntrydate(new Date());
empMapper.addEmp(emp);*/
//从容器中获取EmpMapper接口的实现类对象
EmpMapper empMapper = context.getBean(EmpMapper.class);
Emp emp = new Emp();
emp.setId(8);
emp.setName("吴亦凡");
emp.setAge(25);
emp.setSalary(1000000);
emp.setJob("所长");
emp.setEntrydate(new Date());
empMapper.updateEmp(emp);
}
}
5.根据员工ID删除员工
5.1在EmpMapper中提供方法
@Mapper//添加这个注解,将来mybatis会把EmpMapper接口的实现类对象放入到IOC容器中 (实现类是mybatis自
动提供的,实现类的对象也是mybatis自动创建的, 创建好的对象会放入到IOC容器中)
public interface EmpMapper {
@Select("select * from emp")
public ArrayList<Emp> selectAll();
//完成根据ID查询
//如果要获取参数的值,拼接到sql中, #{参数名}
@Select("select * from emp where id=#{id}")
public Emp findById(Integer id);
//完成员工的新增
//如果要拿对象中的成员变量的值拼接到sql中, #{成员变量的名字}
@Insert("insert into emp(name,age,salary,job,entrydate) values(#{name},#{age},#
{salary},#{job},#{entrydate})")
public void addEmp(Emp emp);
//完成员工的修改
//如果要拿对象中的成员变量的值拼接到sql中, #{成员变量的名字}
@Update("update emp set name=#{name},age=#{age},salary=#{salary},job=#
{job},entrydate=#{entrydate} where id=#{id}")
public void updateEmp(Emp emp);
//根据id删除员工
@Delete("delete from emp where id = #{id}")
public void deleteById(Integer id);
}
5.2 在主启动类App中完成测试
@SpringBootApplication//标识当前类是一个启动类
public class App {
public static void main(String[] args) {
//启动一个spring的程序
//run方法执行完毕后,会返回Spring的IOC容器
ApplicationContext context = SpringApplication.run(App.class,args);
/* System.out.println(context);
//getBean(String name); 根据键获取值
Object obj = context.getBean("userController");
System.out.println(obj);
//getBean(类名.class); 根据类型获取值
UserController obj2 = context.getBean(UserController.class);
System.out.println(obj2);
Animal a = context.getBean(Animal.class);
System.out.println(a);*/
/*ArrayList<Emp> emps = empMapper.selectAll();
System.out.println(emps);*/
/* Emp emp = empMapper.findById(5);
System.out.println(emp);*/
/*Emp emp = new Emp();
emp.setName("王力宏");
emp.setAge(48);
emp.setSalary(1000000);
emp.setJob("所长");
emp.setEntrydate(new Date());
empMapper.addEmp(emp);*/
//从容器中获取EmpMapper接口的实现类对象
EmpMapper empMapper = context.getBean(EmpMapper.class);
/*Emp emp = new Emp();
emp.setId(8);
emp.setName("吴亦凡");
emp.setAge(25);
emp.setSalary(1000000);
emp.setJob("所长");
emp.setEntrydate(new Date());
empMapper.updateEmp(emp);*/
empMapper.deleteById(8);
}
}