From 741a50ad53a6fa16358d1177b1049f21cdf66b74 Mon Sep 17 00:00:00 2001 From: syx_computer Date: Fri, 24 Oct 2025 15:47:09 +0800 Subject: [PATCH] =?UTF-8?q?=E6=96=B0=E7=9A=84=E4=B8=80=E5=A4=A9?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../springbootweb01/SpringbootWeb01Application.java | 2 +- .../springbootweb01/controller/UserController.java | 23 +- .../itheima/springbootweb01/dao/impl/UserDaoImpl.java | 6 + .../springbootweb01/service/lmpl/UserServiceImpl.java | 9 +- .../service/lmpl/UserServiceImpl2.java | 43 +++ .../springbootweb01/controller/UserController.class | Bin 1151 -> 1144 bytes .../springbootweb01/dao/impl/UserDaoImpl.class | Bin 1236 -> 1319 bytes .../service/lmpl/UserServiceImpl.class | Bin 2891 -> 2971 bytes .../service/lmpl/UserServiceImpl2.class | Bin 0 -> 3011 bytes 05. 后端Web基础(数据库)/MySql01.sql | 301 ++++++++++++++++++ .../jdbc-demo/.gitignore | 38 +++ .../jdbc-demo/.idea/.gitignore | 8 + .../jdbc-demo/.idea/encodings.xml | 7 + .../jdbc-demo/.idea/misc.xml | 14 + .../jdbc-demo/.idea/vcs.xml | 6 + .../jdbc-demo/pom.xml | 44 +++ .../jdbc-demo/src/main/java/com/itheima/pojo/User.java | 18 ++ .../jdbc-demo/src/main/java/org/itheima/JdbcTest.java | 70 ++++ 18 files changed, 586 insertions(+), 3 deletions(-) create mode 100644 04. 后端Web基础(基础知识)/springboot-web-01/src/main/java/com/itheima/springbootweb01/service/lmpl/UserServiceImpl2.java create mode 100644 04. 后端Web基础(基础知识)/springboot-web-01/target/classes/com/itheima/springbootweb01/service/lmpl/UserServiceImpl2.class create mode 100644 05. 后端Web基础(数据库)/MySql01.sql create mode 100644 06. 后端Web基础(java操作数据库)/jdbc-demo/.gitignore create mode 100644 06. 后端Web基础(java操作数据库)/jdbc-demo/.idea/.gitignore create mode 100644 06. 后端Web基础(java操作数据库)/jdbc-demo/.idea/encodings.xml create mode 100644 06. 后端Web基础(java操作数据库)/jdbc-demo/.idea/misc.xml create mode 100644 06. 后端Web基础(java操作数据库)/jdbc-demo/.idea/vcs.xml create mode 100644 06. 后端Web基础(java操作数据库)/jdbc-demo/pom.xml create mode 100644 06. 后端Web基础(java操作数据库)/jdbc-demo/src/main/java/com/itheima/pojo/User.java create mode 100644 06. 后端Web基础(java操作数据库)/jdbc-demo/src/main/java/org/itheima/JdbcTest.java diff --git a/04. 后端Web基础(基础知识)/springboot-web-01/src/main/java/com/itheima/springbootweb01/SpringbootWeb01Application.java b/04. 后端Web基础(基础知识)/springboot-web-01/src/main/java/com/itheima/springbootweb01/SpringbootWeb01Application.java index 53d02ab..d07f32d 100644 --- a/04. 后端Web基础(基础知识)/springboot-web-01/src/main/java/com/itheima/springbootweb01/SpringbootWeb01Application.java +++ b/04. 后端Web基础(基础知识)/springboot-web-01/src/main/java/com/itheima/springbootweb01/SpringbootWeb01Application.java @@ -3,7 +3,7 @@ package com.itheima.springbootweb01; import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; -@SpringBootApplication +@SpringBootApplication // public class SpringbootWeb01Application { public static void main(String[] args) { diff --git a/04. 后端Web基础(基础知识)/springboot-web-01/src/main/java/com/itheima/springbootweb01/controller/UserController.java b/04. 后端Web基础(基础知识)/springboot-web-01/src/main/java/com/itheima/springbootweb01/controller/UserController.java index fb117be..501a295 100644 --- a/04. 后端Web基础(基础知识)/springboot-web-01/src/main/java/com/itheima/springbootweb01/controller/UserController.java +++ b/04. 后端Web基础(基础知识)/springboot-web-01/src/main/java/com/itheima/springbootweb01/controller/UserController.java @@ -4,6 +4,9 @@ import cn.hutool.core.io.IoUtil; import com.itheima.springbootweb01.pojo.User; import com.itheima.springbootweb01.service.UserService; import com.itheima.springbootweb01.service.lmpl.UserServiceImpl; +import jakarta.annotation.Resource; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.beans.factory.annotation.Qualifier; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RestController; @@ -28,7 +31,25 @@ public class UserController { * 接受请求 相应数据 */ - private UserService userService = new UserServiceImpl(); +// @Qualifier("userServiceImpl") +// @Autowired +// private UserService userService; + + @Resource(name = "userServiceImpl2") + private UserService userService; + //方式二:构造器注入 +// private final UserService userService; +// @Autowired +// public UserController(UserService userService) { +// this.userService = userService; +// } + + //方式三:setter注入 +// private UserService userService; +// @Autowired +// public void setUserService(UserService userService) { +// this.userService = userService; +// } // @RequestMapping("/list") // public List list() throws Exception{ diff --git a/04. 后端Web基础(基础知识)/springboot-web-01/src/main/java/com/itheima/springbootweb01/dao/impl/UserDaoImpl.java b/04. 后端Web基础(基础知识)/springboot-web-01/src/main/java/com/itheima/springbootweb01/dao/impl/UserDaoImpl.java index 3cc0992..00ec74f 100644 --- a/04. 后端Web基础(基础知识)/springboot-web-01/src/main/java/com/itheima/springbootweb01/dao/impl/UserDaoImpl.java +++ b/04. 后端Web基础(基础知识)/springboot-web-01/src/main/java/com/itheima/springbootweb01/dao/impl/UserDaoImpl.java @@ -2,12 +2,18 @@ package com.itheima.springbootweb01.dao.impl; import cn.hutool.core.io.IoUtil; import com.itheima.springbootweb01.dao.UserDao; +import org.springframework.stereotype.Component; +import org.springframework.stereotype.Repository; +import org.springframework.stereotype.Service; import java.io.InputStream; import java.nio.charset.StandardCharsets; import java.util.ArrayList; import java.util.List; +//@Component // 将当前组件加入到Spring容器中 +//@Repository //将当前组件加入到Spring容器中 +@Service //将当前组件加入到Spring容器中 public class UserDaoImpl implements UserDao { /** * 数据层 diff --git a/04. 后端Web基础(基础知识)/springboot-web-01/src/main/java/com/itheima/springbootweb01/service/lmpl/UserServiceImpl.java b/04. 后端Web基础(基础知识)/springboot-web-01/src/main/java/com/itheima/springbootweb01/service/lmpl/UserServiceImpl.java index f20fab7..4f9de9c 100644 --- a/04. 后端Web基础(基础知识)/springboot-web-01/src/main/java/com/itheima/springbootweb01/service/lmpl/UserServiceImpl.java +++ b/04. 后端Web基础(基础知识)/springboot-web-01/src/main/java/com/itheima/springbootweb01/service/lmpl/UserServiceImpl.java @@ -4,16 +4,23 @@ import com.itheima.springbootweb01.dao.UserDao; import com.itheima.springbootweb01.dao.impl.UserDaoImpl; import com.itheima.springbootweb01.pojo.User; import com.itheima.springbootweb01.service.UserService; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.boot.autoconfigure.AutoConfigureOrder; +import org.springframework.stereotype.Component; +import org.springframework.stereotype.Service; import java.time.LocalDateTime; import java.time.format.DateTimeFormatter; import java.util.List; +@Service public class UserServiceImpl implements UserService { /** * 业务逻辑层 */ - private UserDao userDao = new UserDaoImpl(); + + @Autowired // 自动注入 + private UserDao userDao; @Override public List findAll() { //1.加载Dao层数据 diff --git a/04. 后端Web基础(基础知识)/springboot-web-01/src/main/java/com/itheima/springbootweb01/service/lmpl/UserServiceImpl2.java b/04. 后端Web基础(基础知识)/springboot-web-01/src/main/java/com/itheima/springbootweb01/service/lmpl/UserServiceImpl2.java new file mode 100644 index 0000000..7f6a621 --- /dev/null +++ b/04. 后端Web基础(基础知识)/springboot-web-01/src/main/java/com/itheima/springbootweb01/service/lmpl/UserServiceImpl2.java @@ -0,0 +1,43 @@ +package com.itheima.springbootweb01.service.lmpl; + +import com.itheima.springbootweb01.dao.UserDao; +import com.itheima.springbootweb01.pojo.User; +import com.itheima.springbootweb01.service.UserService; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.context.annotation.Primary; +import org.springframework.stereotype.Component; +import org.springframework.stereotype.Service; + +import java.time.LocalDateTime; +import java.time.format.DateTimeFormatter; +import java.util.List; + +@Service +//@Primary + +public class UserServiceImpl2 implements UserService { + /** + * 业务逻辑层 + */ + + @Autowired // 自动注入 + private UserDao userDao; + @Override + public List findAll() { + //1.加载Dao层数据 + List lines = userDao.findAll(); + + //2.将用户数据封装成User对象 ->list集合 + List userList= lines.stream().map(line -> { + String[] split = line.split(","); + Integer id = Integer.parseInt(split[0]); + String username = split[1]; + String password = split[2]; + String name = split[3]; + Integer age = Integer.parseInt(split[4]); + LocalDateTime updateTime = LocalDateTime.parse(split[5], DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss")); + return new User(id+200, username, password, name, age, updateTime); + }).toList(); + return userList; + } +} diff --git a/04. 后端Web基础(基础知识)/springboot-web-01/target/classes/com/itheima/springbootweb01/controller/UserController.class b/04. 后端Web基础(基础知识)/springboot-web-01/target/classes/com/itheima/springbootweb01/controller/UserController.class index ce4c529f70f4652a6f404d4250eb0759d5d63b59..8d7546fe7004df3a85bc082e474fc2b5d1d3ffc0 100644 GIT binary patch delta 353 zcmX9(%Syvg6r2-NlO|DP>)Xb+n|2Wp1jU7Gp&+!NBDlDgENX2cN$S>Lu;eGim4YCG zAL6HUr*q@t&YUxI?wosn?TP(->{nr7xV8&_~3H(tOrQIlf?DwLx;luqwp=2Vv#kFx; z{1~rKHVgnlV1=k{Aiye7r*HskSSJe_ zsKG2RU$E2Lyyge2FU$zqSFJY+Q)P%#n&)uPDTDNG9rquk-2fYm*eq4yoL07|Y*R7G VJJ@4oU?273(hOYv8U3^t@CW2qEw}&x delta 332 zcmYL^Jxjw-6o#Lh#NITCT0dHCO|@z}m<~?j$Kd25I0(gptCB%zL0agje<9)i3nvAo zh`+!={5RsJCyC&4&ilUioab=A-5)nOENNWDL7KF82E*II?!`DB_2SXw<~lwV@1e2F z_yn5SfH~$BdubAycU4dCPW%NHHA?}@RMU^1nW!(dWro(3X{peBS`YU=7NDfct|&|N z3?->GamU!Apb||{(Hr`5O8EuvDW6=C>gD?l970yb2gN*f8sZddROKyW^~Ghkg6J9V zm5So5``DMj<{z{#z@r>gD~A6-*_n{0Al5U4^di|1)sn=KzR9*Ii#8o|?lk?5xa=cc F_ysTYC$<0p diff --git a/04. 后端Web基础(基础知识)/springboot-web-01/target/classes/com/itheima/springbootweb01/dao/impl/UserDaoImpl.class b/04. 后端Web基础(基础知识)/springboot-web-01/target/classes/com/itheima/springbootweb01/dao/impl/UserDaoImpl.class index 4fc920f0d3c3d3c30c7884edef8eb962135bb006..b13c6be849e2213ceac728e2a34fb9db5a628e14 100644 GIT binary patch delta 137 zcmcb@xtxpZ)W2Q(7#J9A8SFQ5J!5ufWRMIh%`3^wO%2N|&P>Wlb(OHV6G%uOxNFUr<0E=es)%`d4eNYxKcEh@`QPPLx=f!TslaIz*# n1g8uGFM}+DJcHciE*3{oCI)MuZA=U{KpPnt*cccY>=+mTvSufZ delta 53 zcmZ3^b%m4b)W2Q(7#J9A8EiLlJ!771&SJsHJ-L7-f>VfrmqD08o;^|OM?CctGw|G{23m!lTe(Ts+q5Qy8G*%>Hb;&q2XO$brsyGk{~PaK-qh3 zc~0T9gc=2B;NAN>;W>*s3H1sZ(CEyv0kKIzGtN2hS=7@4zl4B-Rs@}2EFEq^NI@80 z2GK4g4a;_;gMme5bRi-^RS-qYnc_x08ZJt>q#%yV&PT4#t>KD-tLSllbKy`QL+uW& zSSl6EW&>kmiCivWSu*-DAmN&V>lk#RuDoXm!xC;PxP{x!C)X=}lp*ZA6~nTO5sWJk zaL1YBU-C%?%^4Iz&L6&d);GPCw~d@WXIRG4hCYX7Ll$&+BtHs?YhB%VS+YS>IGp6fJit==pq!#+FjB#(iou-IRN`Y z0*5I&OU|Iq-J`QaKn|G33?VD^le|j7ZwOt4!N^xghp1*qs)yLEW%v#saES999c`39 z`kCBZWoj>YlA9r;1x@Hc0G;JDfeO`e8X1kak6B6=pd@o7ad<#+^@J7M{E2OW?jDkj z!=oA*i@QjZsZ`$)jij4!JmPKthR&qwiWhOAi0+ii$0t=Gp5Rq6o)A>&_&y=3?zp$N zh(r-L64DQhEJR~I-Y57(pLBq+(km(-6f6__L32wV(uz6+={0TeBeaVM4Q~(H%g>sw zRDj2LLctP-X?CBMf3X)CED>EDy0J`F7ZGU4lEf2D1Ug9qp&u*cClaFXbGg;s+@7HuSKTTLZeXty}x9QLfQ6c*Ojy(ASX?ITTx!7+;r zopw+gGP9g8=(0G;DSKY)^=9d@=p|==))K1E-qRvxKW7cjS)6CU{;NH!8RVkDC5s_0 zE5i1kD{Q}W&FWVa36W5@y0W-Z*6Jfq!NSe3-Kf9OuPHK)#F8G|YI84XTf4@%GEH7t zOfl_e+HR^^+%VBljL5G!V*y;^yYYyD&5ZA;URt%@VNSdom9}r@w zl`{2m6V1fs5}Y#e3e|oY9}&hFmvV6_a$7u&JCYR=ti&e%$L`ABJ&9>d`Zs*eFeK#k z_ZzQ#kWU7V{vcIIxw0jWm*^f&>DiH#JDbx}o@~yYGWPd#o|HEmI9=jQi3>Snm;6M! zBdSN;QBTy^=BlHjGNjfraZsLNNVJ4yBoR>)Wg{k#q>*l!fFo0(x*E;*TDPCT8Ma6A^zEf`~7@;t&A;0X)P%V)9N$WxXCEn!=hy>(vj J+Wuhr{{e^ibeaGF diff --git a/04. 后端Web基础(基础知识)/springboot-web-01/target/classes/com/itheima/springbootweb01/service/lmpl/UserServiceImpl2.class b/04. 后端Web基础(基础知识)/springboot-web-01/target/classes/com/itheima/springbootweb01/service/lmpl/UserServiceImpl2.class new file mode 100644 index 0000000000000000000000000000000000000000..3339935e304ae37dd6fb0e2bcca8d8e82a219cfa GIT binary patch literal 3011 zcmb7GYf}?f7=BJ}3vsIfi;7yMh;j+IN^KRQwuq(B2DJvU)?PeGHn6bSjk_C=-ftiJ zbK04XqMhmIPN)B&|D@ArFM&;AGHqtE=bZPv*XMoDd(Iz!zxWNnHT;l80!ala6=`Gy za%*x!YNoVSwAj4pSh3*8E`Dh@yun5x)y&G1%rqb@bK;TYCR z*|xn+y?o)K=IYLdQPDNC-Y~U$T$Mt?0@srQN|U6U(iRvkc2ZN7b|6jG!T}t_Aq6=V zJ?Iq}{*Q7rRx_;XjA;t=j*J$AAe)|HYDL5KsP$#^DLA6yD2@pnY~LmVuIK2oE^wkv zHl%8$V6)ImJddk5fs+Dh*=U$s0;k*fYfY=-8MdX(MH3o6h0_WKRJ@8ofup;I+jJB9 zx@-ts8HstmS6e);Hu=ELkczWDW*N`+gYLqxfEdqW6k`hVD#kIq~q#b3iR!#b-~y1x{5b& znVH*=W>ddi6F4%m5QP}ni_{gmFePw!i@%A*#ffTlV19nOUY~Z|EDE@);F^l-cvE07 z@WeCfx>mEDy7aW0($kl?o(qQ9saqAj5aQd}T->KN0|f=|2%PP7NE-H9c)EPZ7gW59 z_n05U@|JzngEP-;%wf^*54QxK#64|gU~dupPfEL@y+{>CmlfO*&^m?EYB#Y?BhY`h zY5Dq=4c91}`iy1Sp7al)%lsB?XC>-dHAmLM8iL-6dnil3vpx4u!VP z=lgcGWAyEsa;B`8t8yr6+Cvw7g?k|ghB)J(yE%eJ5;po^=2dLSMiP+gRo6-iH^tF|NCZWva@Xb zqKGulO4BSEo?gg-SM#}eMB!7X5EqI_gzxh#-#lNacm$aPv!O0nmhQ}&(sgxL!8h!% zyH(W|MLRYMz7sgrnU&Xb2qYf_U3{4d+~z;wKejl&&nHHP<6VwPj(oB4TjEONC^)9a zo2*+^m zqn*8vN&AF*34EH%`e1X%5A81k(Z#Ve)_d+Lk3v2表操作<--------------------- +#创建表 +create table user( + id int comment 'ID 唯一', + username varchar(50) comment '用户名', + name varchar(10) comment '姓名', + age int comment '年龄', + gender char(1) comment '性别' +)comment '用户表'; + +#删除表 +drop table user; + +#创建约束表 +create table user( + id int unsigned primary key auto_increment comment 'ID 唯一', -- 主键约束 + username varchar(50) not null unique comment '用户名', -- 唯一非空约束 + name varchar(10) not null comment '姓名', -- 非空约束 + age tinyint unsigned comment '年龄', + gender char(1) default '男' comment '性别' +)comment '用户表'; + + +# 案例:设计员工表 emp +create table emp( + id int unsigned primary key auto_increment comment 'ID 唯一', + username varchar(20) not null unique comment '用户名', -- 唯一非空约束 + password varchar(20) not null default '123456' comment '密码', + name varchar(10) not null comment '姓名', + gender tinyint unsigned not null default 1 comment '性别: 1男 2女', -- 修改为数值默认值 + phone char(11) not null comment '手机号', + job tinyint unsigned comment '职位: 1班主任 2讲师 3学生主管 4教研主管 5咨询师', + salary int unsigned comment '薪资', + entry_date date comment '入职时间', + image varchar(255) comment '图像', + create_time timestamp comment '创建时间', + update_time timestamp comment '更新时间' +)comment '员工表'; + + + +# -------------------------->数据操作<--------------------- +#查询当前所有表 +show tables; + +#查看表结构 +describe emp; + +#查询建表语句 +show create table emp; + +#添加字段 qq varchar(13) +alter table emp add qq varchar(13) comment 'QQ'; + +#修改字段 qq varchar(13) -> varchar(15) +alter table emp modify qq varchar(15) comment 'QQ'; + +#修改字段名 qq -> qq_num +alter table emp change qq qq_num varchar(15) comment 'QQ'; + +#删除字段 qq_num +alter table emp drop qq_num; + +#修改表名 +alter table emp rename to employee; + +#删除表 +drop table employee; + + + +# -------------------------->DML<--------------------- + +-- DML : 数据操作语言 +-- DML : 插入数据 - insert +-- 1. 为 emp 表的 username, password, name, gender, phone 字段插入值 +insert into emp(username, password, name, gender, phone) values('songjiang', '12345678', '宋江', 1, '13888888888'); + +insert into emp(username, password, name, gender, phone) values('songjiang2', '12345678', '宋江', 1, '13888888888'); + +-- 2. 为 emp 表的 所有字段插入值 +#方式一 +insert into emp(id, username, password, name, gender, phone, job, salary, entry_date, image, create_time, update_time) + values (null, 'linchong', '12345678', '林冲', 1, '13888888882', 1, 6000, '2020-01-01', '1.jpg', now(), now()); +#方式二 +insert into emp values(null, 'likui', '12345678', '李逵', 1, '13888888883', 1, 6000, '2020-01-01', '1.jpg', now(), now()); +-- 3. 批量为 emp 表的 username, password, name, gender, phone 字段插入数据 +insert into emp(username, password, name, gender, phone) values + ('lisi', '12345678', '李四', 1, '13888888884'), + ('wangwu', '12345678', '王五', 1, '13888888885'), + ('zhaoliu', '12345678', '赵六', 1, '13888888886'), + ('tianqi', '12345678', '田七', 1, '13888888887'); + +-- DML : 更新数据 - update +-- 1. 将 emp 表的ID为1员工 用户名更新为 'zhangsan', 姓名name字段更新为 '张三' +update emp set username = 'zhangsan', name = '张三' where id = 1; + +-- 2. 将 emp 表的所有员工的入职日期更新为 '2010-01-01' +update emp set entry_date = '2010-01-01'; + + + +-- DML : 删除数据 - delete +-- 1. 删除 emp 表中 ID为1的员工 +delete from emp where id = 1; + +-- 2. 删除 emp 表中的所有员工 +delete from emp; + + + +-- 准备测试数据 +INSERT INTO emp(id, username, password, name, gender, phone, job, salary, image, entry_date, create_time, update_time) +VALUES (1,'shinaian','123456','施耐庵',1,'13309090001',4,15000,'1.jpg','2000-01-01','2024-04-11 16:35:33','2024-04-11 16:35:35'), + (2,'songjiang','123456','宋江',1,'13309090002',2,8600,'2.jpg','2015-01-01','2024-04-11 16:35:33','2024-04-11 16:35:37'), + (3,'lujunyi','123456','卢俊义',1,'13309090003',2,8900,'3.jpg','2008-05-01','2024-04-11 16:35:33','2024-04-11 16:35:39'), + (4,'wuyong','123456','吴用',1,'13309090004',2,9200,'4.jpg','2007-01-01','2024-04-11 16:35:33','2024-04-11 16:35:41'), + (5,'gongsunsheng','123456','公孙胜',1,'13309090005',2,9500,'5.jpg','2012-12-05','2024-04-11 16:35:33','2024-04-11 16:35:43'), + (6,'huosanniang','123456','扈三娘',2,'13309090006',3,6500,'6.jpg','2013-09-05','2024-04-11 16:35:33','2024-04-11 16:35:45'), + (7,'chaijin','123456','柴进',1,'13309090007',1,4700,'7.jpg','2005-08-01','2024-04-11 16:35:33','2024-04-11 16:35:47'), + (8,'likui','123456','李逵',1,'13309090008',1,4800,'8.jpg','2014-11-09','2024-04-11 16:35:33','2024-04-11 16:35:49'), + (9,'wusong','123456','武松',1,'13309090009',1,4900,'9.jpg','2011-03-11','2024-04-11 16:35:33','2024-04-11 16:35:51'), + (10,'lichong','123456','林冲',1,'13309090010',1,5000,'10.jpg','2013-09-05','2024-04-11 16:35:33','2024-04-11 16:35:53'), + (11,'huyanzhuo','123456','呼延灼',1,'13309090011',2,9700,'11.jpg','2007-02-01','2024-04-11 16:35:33','2024-04-11 16:35:55'), + (12,'xiaoliguang','123456','小李广',1,'13309090012',2,10000,'12.jpg','2008-08-18','2024-04-11 16:35:33','2024-04-11 16:35:57'), + (13,'yangzhi','123456','杨志',1,'13309090013',1,5300,'13.jpg','2012-11-01','2024-04-11 16:35:33','2024-04-11 16:35:59'), + (14,'shijin','123456','史进',1,'13309090014',2,10600,'14.jpg','2002-08-01','2024-04-11 16:35:33','2024-04-11 16:36:01'), + (15,'sunerniang','123456','孙二娘',2,'13309090015',2,10900,'15.jpg','2011-05-01','2024-04-11 16:35:33','2024-04-11 16:36:03'), + (16,'luzhishen','123456','鲁智深',1,'13309090016',2,9600,'16.jpg','2010-01-01','2024-04-11 16:35:33','2024-04-11 16:36:05'), + (17,'liying','12345678','李应',1,'13309090017',1,5800,'17.jpg','2015-03-21','2024-04-11 16:35:33','2024-04-11 16:36:07'), + (18,'shiqian','123456','时迁',1,'13309090018',2,10200,'18.jpg','2015-01-01','2024-04-11 16:35:33','2024-04-11 16:36:09'), + (19,'gudasao','123456','顾大嫂',2,'13309090019',2,10500,'19.jpg','2008-01-01','2024-04-11 16:35:33','2024-04-11 16:36:11'), + (20,'ruanxiaoer','123456','阮小二',1,'13309090020',2,10800,'20.jpg','2018-01-01','2024-04-11 16:35:33','2024-04-11 16:36:13'), + (21,'ruanxiaowu','123456','阮小五',1,'13309090021',5,5200,'21.jpg','2015-01-01','2024-04-11 16:35:33','2024-04-11 16:36:15'), + (22,'ruanxiaoqi','123456','阮小七',1,'13309090022',5,5500,'22.jpg','2016-01-01','2024-04-11 16:35:33','2024-04-11 16:36:17'), + (23,'ruanji','123456','阮籍',1,'13309090023',5,5800,'23.jpg','2012-01-01','2024-04-11 16:35:33','2024-04-11 16:36:19'), + (24,'tongwei','123456','童威',1,'13309090024',5,5000,'24.jpg','2006-01-01','2024-04-11 16:35:33','2024-04-11 16:36:21'), + (25,'tongmeng','123456','童猛',1,'13309090025',5,4800,'25.jpg','2002-01-01','2024-04-11 16:35:33','2024-04-11 16:36:23'), + (26,'yanshun','123456','燕顺',1,'13309090026',5,5400,'26.jpg','2011-01-01','2024-04-11 16:35:33','2024-04-11 16:36:25'), + (27,'lijun','123456','李俊',1,'13309090027',5,6600,'27.jpg','2004-01-01','2024-04-11 16:35:33','2024-04-11 16:36:27'), + (28,'lizhong','123456','李忠',1,'13309090028',5,5000,'28.jpg','2007-01-01','2024-04-11 16:35:33','2024-04-11 16:36:29'), + (29,'songqing','123456','宋清',1,'13309090029',5,5100,'29.jpg','2020-01-01','2024-04-11 16:35:33','2024-04-11 16:36:31'), + (30,'liyun','123456','李云',1,'13309090030',NULL,NULL,'30.jpg','2020-03-01','2024-04-11 16:35:33','2024-04-11 16:36:31'); + + + + + + +-- =================== DQL: 基本查询 ====================== +-- 1. 查询指定字段 name,entry_date 并返回 +select name, entry_date from emp; + + +-- 2. 查询返回所有字段 +#方式一:不推荐 +select * from emp; +#方式二:推荐 +select id, username, password, name, gender, phone, job, salary, image, entry_date, create_time, update_time from emp; + +-- 3. 查询所有员工的 name,entry_date, 并起别名(姓名、入职日期) +select name as 姓名, entry_date as 入职日期 from emp; +select name as 姓名, emp.entry_date as 入职日期 from emp; +select name 姓名, emp.entry_date 入职日期 from emp; + +-- 4. 查询已有的员工关联了哪几种职位(不要重复) --distinct 去除重复 +select distinct job from emp; + + +-- =================== DQL: 条件查询 ====================== +-- 1. 查询 姓名 为 柴进 的员工 +select * from emp where name = '柴进'; +select * from emp where name = '柴进'; + +-- 2. 查询 薪资小于等于5000 的员工信息 +select * from emp where salary <= 5000; +select * from emp where salary <= 5000; + +-- 3. 查询 没有分配职位 的员工信息 +select * from emp where job is null; + + +-- 4. 查询 有职位 的员工信息 +select * from emp where job is not null; + + +-- 5. 查询 密码不等于 '123456' 的员工信息 +select * from emp where password != '123456'; + + +-- 6. 查询 入职日期 在 '2000-01-01' (包含) 到 '2010-01-01'(包含) 之间的员工信息\ +select * from emp where entry_date >= '2000-01-01' and entry_date <= '2010-01-01'; + + +-- 7. 查询 入职时间 在 '2000-01-01' (包含) 到 '2010-01-01'(包含) 之间 且 性别为女 的员工信息 +select * from emp where entry_date >= '2000-01-01' and entry_date <= '2010-01-01' and gender = 2; + +-- 8. 查询 职位是 2 (讲师), 3 (学工主管), 4 (教研主管) 的员工信息 +select * from emp where job in (2,3,4); +select * from emp where job = 2 or job = 3 or job = 4; + + +-- 9. 查询 姓名 为两个字的员工信息 +select * from emp where name like '__'; + + +-- 10. 查询 姓 '李' 的员工信息 +select * from emp where name like '李%'; + + +-- 11. 查询 姓名中包含 '二' 的员工信息 +select * from emp where name like '%二%'; + + + +-- =================== DQL: 分组查询 ====================== +-- 聚合函数 +-- 所有null不参与计数 +-- 1. 统计该企业员工数量 +-- cont(字段) +select count(id) from emp; + +-- cont(*) 推荐 +select count(*) from emp; + +-- cont(常量) +select count(123) from emp; + +-- 2. 统计该企业员工的平均薪资 +select avg(salary) from emp; + + +-- 3. 统计该企业员工的最低薪资 +select min(salary) from emp; + + +-- 4. 统计该企业员工的最高薪资 +select max(salary) from emp; + + +-- 5. 统计该企业每月要给员工发放的薪资总额(薪资之和) +select sum(salary) from emp; + + + + + +-- 分组 +-- 1. 根据性别分组 , 统计男性和女性员工的数量 +select gender 性别, count(*) 数量 from emp group by gender; +select gender, count(*) from emp group by gender; + + +-- 2. 先查询入职时间在 '2015-01-01' (包含) 以前的员工 , 并对结果根据职位分组 , 获取员工数量大于等于2的职位 +select job, count(*) from emp where entry_date <= '2015-01-01' group by job having count(*) >= 2; + + + +-- =================== 排序查询 ====================== +-- 1. 根据入职时间, 对员工进行升序排序 +select * from emp order by entry_date; +select * from emp order by entry_date asc; + + +-- 2. 根据入职时间, 对员工进行降序排序 +select * from emp order by entry_date desc; + +-- 3. 根据 入职时间 对公司的员工进行 升序排序 , 入职时间相同 , 再按照 更新时间 进行降序排序 +select * from emp order by entry_date asc, update_time desc; + + + + +-- =================== 分页查询 ====================== +-- 1. 从起始索引0开始查询员工数据, 每页展示5条记录 +select * from emp limit 0,5; + +-- 2. 查询 第1页 员工数据, 每页展示5条记录 +select * from emp limit 0,5; + +-- 3. 查询 第2页 员工数据, 每页展示5条记录 +select * from emp limit 5,5; + +-- 4. 查询 第3页 员工数据, 每页展示5条记录 +select * from emp limit 10,5; + diff --git a/06. 后端Web基础(java操作数据库)/jdbc-demo/.gitignore b/06. 后端Web基础(java操作数据库)/jdbc-demo/.gitignore new file mode 100644 index 0000000..5ff6309 --- /dev/null +++ b/06. 后端Web基础(java操作数据库)/jdbc-demo/.gitignore @@ -0,0 +1,38 @@ +target/ +!.mvn/wrapper/maven-wrapper.jar +!**/src/main/**/target/ +!**/src/test/**/target/ + +### IntelliJ IDEA ### +.idea/modules.xml +.idea/jarRepositories.xml +.idea/compiler.xml +.idea/libraries/ +*.iws +*.iml +*.ipr + +### Eclipse ### +.apt_generated +.classpath +.factorypath +.project +.settings +.springBeans +.sts4-cache + +### NetBeans ### +/nbproject/private/ +/nbbuild/ +/dist/ +/nbdist/ +/.nb-gradle/ +build/ +!**/src/main/**/build/ +!**/src/test/**/build/ + +### VS Code ### +.vscode/ + +### Mac OS ### +.DS_Store \ No newline at end of file diff --git a/06. 后端Web基础(java操作数据库)/jdbc-demo/.idea/.gitignore b/06. 后端Web基础(java操作数据库)/jdbc-demo/.idea/.gitignore new file mode 100644 index 0000000..13566b8 --- /dev/null +++ b/06. 后端Web基础(java操作数据库)/jdbc-demo/.idea/.gitignore @@ -0,0 +1,8 @@ +# Default ignored files +/shelf/ +/workspace.xml +# Editor-based HTTP Client requests +/httpRequests/ +# Datasource local storage ignored files +/dataSources/ +/dataSources.local.xml diff --git a/06. 后端Web基础(java操作数据库)/jdbc-demo/.idea/encodings.xml b/06. 后端Web基础(java操作数据库)/jdbc-demo/.idea/encodings.xml new file mode 100644 index 0000000..aa00ffa --- /dev/null +++ b/06. 后端Web基础(java操作数据库)/jdbc-demo/.idea/encodings.xml @@ -0,0 +1,7 @@ + + + + + + + \ No newline at end of file diff --git a/06. 后端Web基础(java操作数据库)/jdbc-demo/.idea/misc.xml b/06. 后端Web基础(java操作数据库)/jdbc-demo/.idea/misc.xml new file mode 100644 index 0000000..df00c07 --- /dev/null +++ b/06. 后端Web基础(java操作数据库)/jdbc-demo/.idea/misc.xml @@ -0,0 +1,14 @@ + + + + + + + + + + \ No newline at end of file diff --git a/06. 后端Web基础(java操作数据库)/jdbc-demo/.idea/vcs.xml b/06. 后端Web基础(java操作数据库)/jdbc-demo/.idea/vcs.xml new file mode 100644 index 0000000..b2bdec2 --- /dev/null +++ b/06. 后端Web基础(java操作数据库)/jdbc-demo/.idea/vcs.xml @@ -0,0 +1,6 @@ + + + + + + \ No newline at end of file diff --git a/06. 后端Web基础(java操作数据库)/jdbc-demo/pom.xml b/06. 后端Web基础(java操作数据库)/jdbc-demo/pom.xml new file mode 100644 index 0000000..06f2828 --- /dev/null +++ b/06. 后端Web基础(java操作数据库)/jdbc-demo/pom.xml @@ -0,0 +1,44 @@ + + + 4.0.0 + + org.itheima + jdbc-demo + 1.0-SNAPSHOT + + + 17 + 17 + UTF-8 + + + + + com.mysql + mysql-connector-j + 8.0.33 + + + + org.junit.jupiter + junit-jupiter + 5.9.3 + test + + + + org.projectlombok + lombok + 1.18.30 + + + junit + junit + RELEASE + compile + + + + \ No newline at end of file diff --git a/06. 后端Web基础(java操作数据库)/jdbc-demo/src/main/java/com/itheima/pojo/User.java b/06. 后端Web基础(java操作数据库)/jdbc-demo/src/main/java/com/itheima/pojo/User.java new file mode 100644 index 0000000..b62396a --- /dev/null +++ b/06. 后端Web基础(java操作数据库)/jdbc-demo/src/main/java/com/itheima/pojo/User.java @@ -0,0 +1,18 @@ +package com.itheima.pojo; + + +import lombok.AllArgsConstructor; +import lombok.Data; +import lombok.NoArgsConstructor; + +@Data +@AllArgsConstructor +@NoArgsConstructor +public class User { + private Integer id; + private String username; + private String password; + private String name; + private Integer age; + +} diff --git a/06. 后端Web基础(java操作数据库)/jdbc-demo/src/main/java/org/itheima/JdbcTest.java b/06. 后端Web基础(java操作数据库)/jdbc-demo/src/main/java/org/itheima/JdbcTest.java new file mode 100644 index 0000000..e3e0b4d --- /dev/null +++ b/06. 后端Web基础(java操作数据库)/jdbc-demo/src/main/java/org/itheima/JdbcTest.java @@ -0,0 +1,70 @@ +package org.itheima; + +import com.itheima.pojo.User; +import org.junit.Test; + +import java.sql.*; + +public class JdbcTest { + @Test + public void testUpdate() throws Exception { + //1.注册驱动 + Class.forName("com.mysql.cj.jdbc.Driver"); + + //2.获取连接 + String url = "jdbc:mysql://localhost:3306/web01"; + String username = "root"; + String password = "123456"; + Connection connection = DriverManager.getConnection(url, username, password); + + //3.获取数据库操作对象 + Statement statement = connection.createStatement(); + + //4.执行sql语句 + int i = statement.executeUpdate("update user set age = 25 where id = 1");//DML + System.out.println("SQL执行完毕" + i); + + //5.释放资源 + statement.close(); + connection.close(); + } + + @Test + public void testSelect() throws Exception { + String url = "jdbc:mysql://localhost:3306/web01"; + String username = "root"; + String password = "123456"; + + Connection conn = null; + PreparedStatement stmt = null; + ResultSet rs = null; //封装查询返回的结果集 + + try { + //1.注册驱动 + Class.forName("com.mysql.cj.jdbc.Driver"); + //2.获取连接 + conn = DriverManager.getConnection(url, username, password); + //3.执行查询 + String sql = "select id, username, password, name, age FROM user WHERE username = ? AMD password = ?"; + stmt = conn.prepareStatement(sql); + + stmt.setString(1, "daqiao"); + stmt.setString(2, "123456"); + + rs = stmt.executeQuery(); + + //4.处理结果 + while (rs.next()) { + User user = new User( + rs.getInt("id"), + rs.getString("username"), + rs.getString("password"), + rs.getString("name"), + rs.getInt("age") + ); + System.out.println(user); + + } + } + } +}