第一次课后作业
1.jsp记事簿练习
记事簿代码如下:
<%@ page language="java" import="java.util.*" pageEncoding="UTF-8" %>
<!DOCTYPE html>
<html>
<head>
<title>JSP - Hello World</title>
</head>
<body>
<form action="output.jsp" method="post">
<ul style="list-style:none;line-height:30px">
<table border="1">
<tr>
<td>姓名:</td>
<td><input type="text" name="name"></td>
</tr>
<tr>
<td>性别:</td>
<td>
<input type="radio" id="male" name="gender" value="male">
<label for="male">男</label>
<input type="radio" id="female" name="gender" value="female">
<label for="female">女</label>
</td>
</tr>
<tr>
<td>爱好:</td>
<td>
<input type="checkbox" name="like">篮球
<input type="checkbox" name="like">旅游
<input type="checkbox" name="like">音乐
<input type="checkbox" name="like">摄影
</td>
</tr>
<tr>
<td>E-mail:</td>
<td><input type="text" name="home"></td>
</tr>
<tr>
<td></td>
<td><input type="submit" value="Submit"><input type="reset" value="全部重写"></td>
</tr>
</table>
</ul>
</form>
</body>
网页效果图如下:
2.jsp 实现用户登录认证
<%@ page language="java" import="java.util.*" pageEncoding="UTF-8" %>
<!DOCTYPE html>
<% String str=request.getParameter("zhanghao");
String mima=request.getParameter("mima");
if(null!=str){
if(str.equals("tom")&&mima.equals("123456")){
out.println("您好,tom!");
}
else{
out.println("您输入的账号密码有误,请重新输入!");
}
} %>
<html>
<head>
<title>JSP - Hello World</title>
</head>
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Document</title>
</head>
<body>
<form action="2-1.jsp" method="post">
账号:<input type="text" name="zhanghao"><br>
密码:<input type="password" name="mima"><br>
<input type="submit" name="submit" value="登录">
</form>
</body>
</html>
二、第二次课后作业
1.form表单练习
2.用navicat创建数据库和数据库表格
3.新建maven项目并修改密码
package com.dzy;
import java.sql.*;
public class helloworld {
public static void main(String[] args) {
try {
Class.forName("com.mysql.jdbc.Driver");
Connection c = DriverManager
.getConnection(
"jdbc:mysql://127.0.0.1:3306/how2java?characterEncoding=UTF-8",
"root", "520819");
Statement s = c.createStatement();
// 准备sql语句
// 注意: 字符串要用单引号'
String sql = "select * from category_";
ResultSet rs =s.executeQuery(sql);
while (rs.next()) {
System.out.println( rs.getString(2) );
}
} catch (ClassNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
第三次课后作业
1.请求响应练习
原始的httpservletrequest
复杂实体参数传递
数组集合参数
日期参数
json参数
路径参数
@responsebody响应数据
2.product.html
productcontroller
product
prodcut.html
运行结果
3.表单内容传递
package com.dzyspringboot.demos.web.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
@RestController
public class FormController {
@RequestMapping("/output.jsp")
public String simpleParam(String name,String gender,String hobby,String Email){
System.out.println("您输入的信息是"+name+":"+gender+":"+hobby+":"+Email);
return "OK";
}
}
<%@ page language="java" import="java.util.*" pageEncoding="UTF-8" %>
<html>
<head>
<title>JSP - Hello World</title>
</head>
<body>
<form action="output.jsp" method="post">
<ul style="list-style:none;line-height:30px">
<table border="1">
<tr>
<td>姓名:</td>
<td><input type="text" name="name"></td>
</tr>
<tr>
<td>性别:</td>
<td>
<input type="radio" id="male" name="gender" value="male">
<label for="male">男</label>
<input type="radio" id="female" name="gender" value="female">
<label for="female">女</label>
</td>
</tr>
<tr>
<td>爱好:</td>
<td>
<input type="checkbox" name="like">篮球
<input type="checkbox" name="like">旅游
<input type="checkbox" name="like">音乐
<input type="checkbox" name="like">摄影
</td>
</tr>
<tr>
<td>E-mail:</td>
<td><input type="text" name="home"></td>
</tr>
<tr>
<td></td>
<td><input type="submit" value="Submit"><input type="reset" value="全部重写"></td>
</tr>
</table>
</ul>
</form>
第四次课后作业
读取xml文件的三种方式
1.常规方式--controller控制器不分层
网页展示
poetcontroller代码
package com.dzyspringboot.demos.web.Controller;
import com.dzyspringboot.demos.web.utils.XmlParser1Utils;
import com.dzyspringboot.pojo.Poet;
import com.dzyspringboot.pojo.Result;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import java.util.List;
@RestController
public class PoetController {
@RequestMapping("/listPoet")
public Result list(){
//加载并解析xml文件
String file=this.getClass().getClassLoader().getResource("poet.xml.tld").getFile();//获取到的是poet.xml的磁盘路径
System.out.println(file);
List<Poet>poetList =XmlParser1Utils.parse(file,Poet.class);//解析到了file里面封装Poet文件里面
//对数据进行转换处理gender
//基于stream流遍历文件拿到文件内的信息
poetList.stream().forEach(poet -> {
//gender
String gender=poet.getGender();
if ("1".equals(gender)){
poet.setGender("男");
}
else if ("2".equals(gender)){
poet.setGender("女");
}
});
//响应数据
return Result.success(poetList);
}
}
poet代码
package com.dzyspringboot.pojo;
public class Poet {
private String author;
private String gender;
private String dynasty;
private String style;
private String title;
public Poet(){
}
public Poet(String author,String gender,String dynasty,String style,String title){
this.author=author;
this.gender=gender;
this.dynasty=dynasty;
this.style=style;
this.title=title;
}
public String getAuthor() {
return author;
}
public void setAuthor(String author) {
this.author = author;
}
public String getGender() {
return gender;
}
public void setGender(String gender) {
this.gender = gender;
}
public String getDynasty() {
return dynasty;
}
public void setDynasty(String dynasty) {
this.dynasty = dynasty;
}
public String getStyle() {
return style;
}
public void setStyle(String style) {
this.style = style;
}
public String getTitle() {
return title;
}
public void setTitle(String title) {
this.title = title;
}
@Override
public String toString() {
return "Poet{" +
"author='" + author + '\'' +
", gender=" + gender+
", title='" + title + '\'' +
", dynasty='" + dynasty + '\'' +
", style='" + style + '\'' +
'}';
}
}
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>诗人和辞赋家</title>
</head>
<link rel="stylesheet" href="element-ui/index.css">
<script src="./js/vue.js"></script>
<script src="./element-ui/index.js"></script>
<script src="./js/axios-0.18.0.js"></script>
<body>
<h1 align="center">诗人信息列表展示</h1>
<div id="app">
<el-table :data="tableData" style="width: 100%" stripe border >
<el-table-column prop="author" label="姓名" align="center" min-width="20%"></el-table-column>
<el-table-column prop="gender" label="性别" align="center" min-width="20%"></el-table-column>
<el-table-column prop="dynasty" label="朝代" align="center" min-width="20%"></el-table-column>
<el-table-column prop="title" label="头衔" align="center" min-width="20%"></el-table-column>
<el-table-column prop="style" label="风格" align="center" min-width="20%"></el-table-column>
</el-table>
</div>
</body>
<style>
.el-table .warning-row {
background: oldlace;
}
.el-table .success-row {
background: #f0f9eb;
}
</style>
<script>
new Vue({
el: "#app",
data() {
return {
tableData: []
}
},
mounted(){
axios.get('/listPoet').then(res=>{
if(res.data.code){
this.tableData = res.data.data;
}
});
},
methods: {
}
});
</script>
</html>
2.三层架构
代码结构
package com.dzyspringboot.demos.web.dao;
import com.dzyspringboot.demos.web.utils.XmlParser1Utils;
import com.dzyspringboot.pojo.Poet;
import java.util.List;
public interface PoetDao {
public List<Poet> listPoet() ;
}
package com.dzyspringboot.demos.web.dao.imp;
import com.dzyspringboot.demos.web.dao.PoetDao;
import com.dzyspringboot.demos.web.utils.XmlParser1Utils;
import com.dzyspringboot.pojo.Poet;
import java.util.List;
public class PoetDaoA implements PoetDao {
@Override
public List<Poet> listPoet() {
//加载并解析xml文件
String file=this.getClass().getClassLoader().getResource("poet.xml.tld").getFile();//获取到的是poet.xml的磁盘路径
System.out.println(file);
List<Poet>poetList = XmlParser1Utils.parse(file,Poet.class);//解析到了file里面封装Poet文件里面
return poetList;
}
}
package com.dzyspringboot.demos.web.servic.imp;
import com.dzyspringboot.demos.web.dao.PoetDao;
import com.dzyspringboot.demos.web.dao.imp.PoetDaoA;
import com.dzyspringboot.demos.web.servic.PoetService;
import com.dzyspringboot.pojo.Poet;
import java.util.List;
public class PoetServiceA implements PoetService {
//创建一个poetdao接口类的实例
private PoetDao poetDao=new PoetDaoA();
@Override
public List<Poet> listPoet() {
//引入dao实例
List<Poet> poetList=poetDao.listPoet();
poetList.stream().forEach(poet -> {
//gender
String gender=poet.getGender();
if ("1".equals(gender)){
poet.setGender("男");
}
else if ("2".equals(gender)){
poet.setGender("女");
}
});
return poetList;
}
}
package com.dzyspringboot.demos.web.servic;
import com.dzyspringboot.pojo.Poet;
import java.util.List;
public interface PoetService {
public List<Poet> listPoet();
}
package com.dzyspringboot.demos.web.Controller;
import com.dzyspringboot.demos.web.servic.PoetService;
import com.dzyspringboot.demos.web.servic.imp.PoetServiceA;
import com.dzyspringboot.demos.web.utils.XmlParser1Utils;
import com.dzyspringboot.pojo.Poet;
import com.dzyspringboot.pojo.Result;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import java.util.List;
@RestController
public class PoetController {
private PoetService poetService=new PoetServiceA();
@RequestMapping("/listPoet")
public Result list(){
//调用service,获取数据
List<Poet> poetList=poetService.listPoet();
//响应数据
return Result.success(poetList);
}
}
第五次课后作业
database的配置
案例代码完整补充
EmpMapper
package dzymybatis2.dzymybatis2.mapper;
import dzymybatis2.dzymybatis2.pojo.Emp;
import org.apache.ibatis.annotations.*;
import java.time.LocalDate;
import java.time.LocalDateTime;
import java.util.List;
@Mapper//自动创建该接口的代理对象,并且放入ioc容器里面
public interface EmpMapper {
//根据ID删除数据
@Delete("delete from tb_emp where id=#{id}")
//public void delete(Integer id);
//返回值代表的操作次数
public int delete(Integer id);
//新增员工
@Options(useGeneratedKeys = true, keyProperty = "id")
@Insert("insert into tb_emp(username, name, gender, image, job, entrydate, dept_id, create_time, update_time)" +
"value (#{username},#{name},#{gender},#{image},#{job},#{entrydate},#{deptId},#{createTime},#{updateTime})")
public void insert(Emp emp);
//更新员工信息
@Update("update tb_emp set username=#{username},name=#{name},gender=#{gender},image=#{image}" +
"job=#{job},entrydate=#{entrydate},dept_id='#{deptId}',create_time=#{createTime},update_time=#{updateTime} where id=#{id}")
public void update(Emp emp);
//根据ID查询员工
//@Select("select *from tb_emp where id=#{id};")
//public Emp getById(Integer id);
//给字段取别名,与实体属性名一致即可
/*@Select("select id,username,password,name,gender,image,job,entrydate," +
"dept_id deptId,create_time createTime,update_time updateTime from tb_emp where id=#{id};")
public Emp getById(Integer id);
*/
//@Results注解手动封装
/* @Results(
{
@Result(column="dept_id",property="deptId"),
@Result(column="create_time",property="createTime"),
@Result(column="update_time",property="updateTime"),
}
)
@Select("select *from tb_emp where id=#{id}")
public Emp getById(Integer id);
}*/
//驼峰命名
@Select("select *from tb_emp where id=#{id}")
public Emp getById(Integer id);
//条件查询员工信息注意此处不可以用#在%之后
@Select("select*from tb_emp where name like '%${name}%' and gender=#{gender} " +
"and entrydate between #{begin} and #{end} order by update_time desc")
public List<Emp> list(String name, short gender, LocalDate begin,LocalDate end);
}
application
package dzymybatis2.dzymybatis2;
import dzymybatis2.dzymybatis2.mapper.EmpMapper;
import dzymybatis2.dzymybatis2.pojo.Emp;
import org.apache.ibatis.annotations.Delete;
import org.apache.ibatis.annotations.Select;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import java.time.LocalDate;
import java.time.LocalDateTime;
import java.util.List;
@SpringBootTest
class Dzymybatis2ApplicationTests {
@Autowired
private EmpMapper empMapper;
@Test
public void testDelete(){
int delete=empMapper.delete(16);
System.out.println(delete);
}
@Test
public void testInsert(){
Emp emp=new Emp();
emp.setUsername("tom1");
emp.setName("汤姆1");
emp.setImage("1.jpg");
emp.setGender((short)1);
emp.setJob((short)1);
emp.setEntrydate(LocalDate.of(2024,1,1));
emp.setCreateTime(LocalDate.now());
emp.setUpdateTime(LocalDate.now());
emp.setDeptId(1);
//执行新增员工信息
empMapper.insert(emp);
System.out.println(emp.getId());
}
//更新与员工信息
@Test
public void testUpdate(){
//构造员工对象
Emp emp=new Emp();
emp.setId(2);
emp.setUsername("tom2");
emp.setName("汤姆2");
emp.setImage("1.jpg");
emp.setGender((short)1);
emp.setJob((short)1);
emp.setEntrydate(LocalDate.of(2024,1,1));
emp.setUpdateTime(LocalDate.now());//更新时间每次都需要操作
emp.setDeptId(1);
//执行新增员工信息
empMapper.update(emp);
}
//根据ID查询员工
@Test
public void testGetById(){
Emp emp=empMapper.getById(21);
System.out.println(emp);
}
//员工信息的列表查询
@Test
public void testList(){
List<Emp> empList=empMapper.list("张",(short)1,LocalDate.of(2010,1,1),LocalDate.of(2020,1,1));
System.out.println(empList);
}
}
lombok库的使用
package com.itheima.pojo;
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;
import java.time.LocalDate;
import java.time.LocalDateTime;
@Data
@NoArgsConstructor
@AllArgsConstructor
public class Emp {
private Integer id;
private String username;
private String password;
private String name;
private Short gender;
private String image;
private Short job;
private LocalDate entrydate;
private Integer deptId;
private LocalDateTime createTime;
private LocalDateTime updateTime;
}
表格创建
诗人表格SQL代码
create table poet (
id int unsigned primary key auto_increment comment 'ID',
author varchar(20) not null unique comment '诗人',
gender tinyint unsigned not null comment '性别, 说明: 1 男, 2 女',
dynasty varchar(20) not null comment '朝代',
title varchar(20) not null unique comment '头衔',
style varchar(20) not null unique comment '风格'
) comment '诗人表';
INSERT INTO poet
(id,author,gender,dynasty,title,style ) VALUES
(1,'陶渊明','1','东晋至南朝宋','诗人和辞赋家','古今隐逸诗人之宗'),
(2,'王维','1','唐代','诗佛','空灵,寂静'),
(3,'李白','1','唐代','诗仙','豪放飘逸的诗风和丰富的想象'),
(4,'李商隐','1','唐代','诗坛鬼才','无'),
(5,'李清照','2','宋代','女词人','婉约风格'),
(6,'杜甫','1','唐代','诗圣','反应社会现实和人民疾苦'),
(7,'苏轼','1','宋代','文学家,书画家','清新豪健的诗风和独特的艺术');
delete from poet where id=7;
insert into poet(id, author, gender, dynasty, title, style) value ('8','温庭筠','1','明代','词人','清新脱俗,文辞温雅');
update poet set id=7,author='苏轼',gender='1',dynasty='宋代',title='文学家,书画家',style='清新豪健的诗风和独特的艺术';
poetmapper
package dzymybatis2.dzymybatis2.mapper;
import dzymybatis2.dzymybatis2.pojo.Poet;
import org.apache.ibatis.annotations.*;
@Mapper
public interface PoetMapper {
//根据ID删除数据
@Delete("delete from poet where id=#{id};")
//public int delete(Integer id);
//返回操作的次数
public int delete(Integer id);
//新增诗人
@Options(useGeneratedKeys = true,keyProperty = "id")
@Insert("insert into poet(id, author, gender, dynasty, title, style)" +
" value (#{id},#{author},#{gender},#{dynasty},#{title},#{style});")
public void insert(Poet poet);
//更新诗人信息
@Update("update poet set id=#{id},author=#{author},gender=#{gender}," +
"dynasty=#{dynasty},title=#{title},style=#{style};")
public void update(Poet poet);
}
test
private PoetMapper poetMapper;
@Test
public void testDelete1(){
int delete=poetMapper.delete(7);
System.out.println(delete);
}
@Test
public void testInsert(){
Poet poet=new Poet();
poet.setAuthor("温庭筠");
poet.setGender((short)1);
poet.setDynasty("明朝");
poet.setTitle("词人");
poet.setStyle("清新脱俗,文辞温雅");
//执行新增员工信息
poetMapper.insert(poet);
System.out.println(poet.getId());
}
@Test
public void testUpdate(){
Poet poet=new Poet();
poet.setId(7);
poet.setAuthor("苏轼");
poet.setGender((short)1);
poet.setDynasty("宋代");
poet.setTitle("文学家,书画家");
poet.setStyle("清新豪健的诗风和独特的艺术");
poetMapper.update(poet);
}