第三阶段第一周笔记及代码

2020/6/28–第一天—介绍以及软件

如何使用sts以及配置maven环境,创建的是spring项目## 二级目录

2020/6/29第二天

2020/6/30–第三天–Spring Boot 整合连接池

上午从十点二十四开始缺,需要补,
下午说的Spring Boot 整合连接池

2020/7/1–第四天–mybatis框架的整合

上午:
开始说的debug大概到十点半这样,整理debug的方式
四大引用的认识与举例
下午:
基于spring对mybatis框架的整合。主要是04中GoodsDao中的内容通过debug来显示出哪里使用了mybatie因为形式和内容都和spring差不多看不出来需要借助工具来解释一下,
出现的问题有》》导入mybites时pom.xml文件出错然后右键spring选择edit startes,如果spring不行就换aliyun.com

单元测试报错记得看一下是不是这几个错误在这里插入图片描述
业务进阶分析及实现方面较难需要再看

2020/7/2–第五天–自习排除问题

1.List findGoods();

List指的是集合.<>是泛型,里面bai指定了这个集du合中存放的是什么数据.
查询显示中需要数据类型所以创建Goods类来描述并限定类型
@Select(“select id,name,remark,createdTime from tb_goods”)
List findObjects();

2.@Insert问题(接口类中插入方式未解决)

spring整合mybatis后处理sql有两种方式

  1. 在GoodsDao接口类中书写处理sql语句需要注解@Delete@Select@Update相对较麻烦
    首先书写sql的注解,然后在GoodsService接口类中定义,接口实现类GoodsServiceImpl要重写(Override)方法,最后在测试类GoodsDaoTests中先
    @Autowired
    private GoodsService goodsService;
    然后在使用@Test即可
  2. 在Goodsmapper.xml中书写处理sql语句要在标签中书写对应信息较简单
    在mapper标签中书写对应的sql语句标签注意id,书写好后需要在接口类GoodsDao中定义id这样才能在测试类GoodsDaoTests中使用,但是需要在测试类上加上
    @Autowired
    private GoodsDao gs;
    然后就可以在test中进行单元测试

接口类以及测试类代码如下
Goods:接口类:
com.cy.pj.goods.pojo.Goods是类的实现

package com.cy.pj.goods.pojo;

import java.sql.Date;

public class Goods {
	private Long id;//id bigint primary key auto_increment
	private String name;//name varchar(100) not null
	private String remark;//remark text
	private Date createdTime;//createdTime datetime
	//余下的是set和get方法以及重写我就不延伸了

com.cy.pj.goods.dao.GoodsDao是接口类

package com.cy.pj.goods.dao;
import java.util.List;

import org.apache.ibatis.annotations.Delete;
import org.apache.ibatis.annotations.Insert;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Param;
/**
 * @Mapper 注解由mybatis框架定义,用于描述数据层接口对象
 * ,系统底层启动mybatis框架会基于@Mapper注解的描述,创建
 * 其接口实现类,并将实现类对象交给spring管理。
 * @author qilei
 */
import org.apache.ibatis.annotations.Select;
import org.apache.ibatis.annotations.Update;

import com.cy.pj.goods.pojo.Goods;
@Mapper
public interface GoodsDao {
	  /**
	   * 基于id执行批量删除操作
	   * @param ids
	   * @return
	   */
	  //int deleteObjects(@Param("ids")Integer ...ids);//早期版本需要基于@Param注解定义参数名
	  //通过Mapper进行插入
	  int deleteObjects(Integer... ids);//sql映射中可以使用array,ids参数名来接收方法参数数据 
	  //通过Mapper进行插入
	  int insert1();
	  
      /**
       * 基于id删除数据库中的商品信息
       * @param id
       * @return
       */
	  @Delete("delete from tb_goods where id=#{id}")
	  int deleteById(Integer id);
	  //方式一:直接在Dao里面书写@Delete的内容
	  //但是删除需要参数所以添加 int deleteById(Integer id);
	  @Select("select id,name,remark,createdTime from tb_goods")
	  List<Goods> findObjects();
	//方式一:直接在Dao里面书写@@Select的内容但是查看需要集合来限定
	  //也就需要创建Goods类
	  @Update("update tb_goods set name='zhangsan' where id=#{id}")
	  int updateById(Integer idforname);
	  //方式二:在.xml中解决sql语言问题
}

GoodsMapper.xml里面是另一种实现sql语句的方式

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE mapper
  PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
  "http://mybatis.org/dtd/mybatis-3-mapper.dtd">
  <!-- 
	不同Mapper文件的namespace值应该保证唯一
	在程序中通过[ namespace + id ]定位到要执行哪一条SQL语句
 -->
  <mapper namespace="com.cy.pj.goods.dao.GoodsDao">
      <delete id="deleteObjects">
          delete from tb_goods
          <where>
            <if test="ids!=null and ids.length!=0">
              id in <!-- (1,2,3,4) -->
               <foreach  collection="ids"
                open="(" separator="," close=")" item="id">
                #{id}
               </foreach>
            </if>
            or 1=2
          </where>
      </delete>
    <insert id="insert1">
        insert into tb_goods value(null,'wdl','tl',now())
    </insert>
  </mapper>

com.cy.pj.goods.dao.GoodsDaoTests是测试类

package com.cy.pj.goods.dao;

import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;

@SpringBootTest
public class GoodsDaoTests {
	//has a
	@Autowired
	private GoodsDao goodsDao;//请问这个引用指向的对象是谁?你怎么知道的
	@Test
	void testDeleteById() {
		int rows=goodsDao.deleteById(10);//请此操作需要连接吗?从哪里来?
		System.out.println("delete.rows="+rows);
	}//在GoodsDao里面直接书写的方式一
	@Test
	void testDeleteObjects() {
		//执行goodsDao.deleteObjects方法时候,系统会:
		//1)基于接口类全名找到与其namespace对应的映射文件。
		int rows=goodsDao.deleteObjects(10,11,12);
		System.out.println("delete.rows="+rows);
	}//在GoodsMapper.xml里面书写的方式二
}

service接口类:
com.cy.pj.goods.service.GoodsService是接口类

package com.cy.pj.goods.service;

import java.util.List;

import org.apache.ibatis.annotations.Delete;
import org.apache.ibatis.annotations.Insert;
import org.apache.ibatis.annotations.Select;
import org.apache.ibatis.annotations.Update;

import com.cy.pj.goods.pojo.Goods;

public interface GoodsService {

	 int deleteById(Integer id);//@Delete
	 
	 List<Goods> findGoods();// @Select
	 
	 int updateById(Integer idforname);// @Update

}

com.cy.pj.goods.service.GoodsServiceImpl是接口实现类

package com.cy.pj.goods.service;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.cy.pj.goods.dao.GoodsDao;
import com.cy.pj.goods.pojo.Goods;

@Service //@Component
public class GoodsServiceImpl implements GoodsService {
	@Autowired
	private GoodsDao goodsDao;
	@Override
	public int deleteById(Integer id) {
		int rows=goodsDao.deleteById(id);//主要内容利用方式一
		return rows;
	}

	@Override
	public List<Goods> findGoods() {
		long start=System.currentTimeMillis();//无关紧要--计数
		
		List<Goods> list=goodsDao.findObjects();//主要内容利用方式一
		
		long end=System.currentTimeMillis();//无关紧要--计数
		System.out.println("query time:"+(end-start));//无关紧要--计数
		return list;
	}

	@Override
	public int updateById(Integer idforname) {
		// TODO Auto-generated method stub
		int rows = goodsDao.updateById(idforname);
		return rows;
	}


}

com.cy.pj.goods.dao.GoodsDaoTests是测试类

package com.cy.pj.goods.service;

import java.util.HashMap;
import java.util.List;

import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;

import com.cy.pj.goods.dao.GoodsDao;
import com.cy.pj.goods.pojo.Goods;

@SpringBootTest
public class GoodsServiceTests {

	@Autowired
	private GoodsService goodsService;
	@Autowired
	private GoodsDao gs;
	
	@Test
	void testDeleteById() {
		int rows=goodsService.deleteById(15);
		System.out.println("rows="+rows);
	}
	
	@Test
	public void testFindGoods() {
		List<Goods> list=goodsService.findGoods();
            list=goodsService.findGoods();
		//System.out.println(list);//toString()
		for(Goods g:list) {
			System.out.println(g);//toString()
		}
      }//
	@Test
	void testUpdateById() {
		int rows=goodsService.updateById(1);
		System.out.println("rows="+rows);
	}
	
	@Test//访问GoodsDao接口中xml中的方式
	void testdeleteObjectsUseMapper_xml() {
		int rows = gs.deleteObjects(1,2,3);
		System.out.println(rows);
	}
	@Test//访问GoodsDao接口中xml中的方式
	void testinsertObjectsUseMapper_xml() {
		int rows = gs.insert1();参数在mapper.xml里面书写
		System.out.println("rows="+rows);
	}
}

2020/7/3–第六天–基于Spring Boot脚手架整合SpringMVC应用

与前端交互通过Thymeleaf(取代jsp)

控制器(Controller)- 负责获取请求,处理请求,响应结果。
模型(Model) - 实现业务逻辑,数据逻辑实现。

仍然在04里面接着书写今天的内容

1 编辑pom.xml文件,添加web依赖,Thymeleaf依赖
Web依赖(提供了Spring MVC核心API,同时会嵌入一个Tomcat服务器)
Thymeleaf依赖(提供了一个视图解析器对象以及数据绑定机制)

2 配置Spring MVC 核心对象在application.properties文件中添加视图解析器配置
spring.thymeleaf.prefix=classpath:/templates/pages/
spring.thymeleaf.suffix=.html

3 在src/main/resources目录下创建templates/pages目录
目录里创建goods.xml文件里面是要显示的内容

<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Insert title here</title>
</head>
<body>
XXXXXXXXXXXXX
</body>
</html>

4编写GoodsController类并将其交给spring管理,Controller在SpringMVC 规范中通常称之为Handler(处理器)在企 业中有时也会将此对象理解为一个后端控制器

package com.cy.pj.goods.controller;
@Controller
@RequestMapping("/goods/")
public class GoodsController {
 @RequestMapping("doGoodsUI")
public String doGoodsUI() {
 return "goods"; } }

练习一: 将数据库中的商品数据查询出来更新到页面上。(出现问题的原因:注解,拼写)

第一步:在上述GoodsController 中添加Model 以及接口对象

@Autowired
private GoodsService goodsService;
@RequestMapping("doGoodsUI")
public String doGoodsUI(Model model){
 	List<Goods> list = goodsService.findObjects();
 	model.addAttribute("list,list");
 	return "goods";
}

第二步:在.html中显示,利用的是thymeleaf中的方式

	<table>
		<thead>
			<th>id</th>
			<th>name</th>
			<th>remark</th>
			<th>createdTime</th>
		</thead>
		<tbody>
			<tr th:each="g:${list}">
				<td th:text="${g.id}">1</td>
				<td th:text="${g.name}">MySql</td>
				<td th:text="${g.remark}">DBMS</td>
				<td th:text="${g.createdTime}">2020/4/4</td>
			</tr>
		</tbody>
	</table>

练习二:基于ID删商品库中的商品信息。

	@RequestMapping("doDeleteById")
	//http://localhost:8080/goods/dodoDeleteById?id=9
	public String doDeleteById(Integer id){
		goodsService.deleteById(id);
		return "redirect:doGoodsUI";
	}//可以直接使用,但是需要美化使用按钮就好啦
<th>operation</th>>
...
<td><a th:href="@{/goods/doDeleteById(id=${g.id})}">delete</a></td>>

练习三:将页面用户输入的商品信息写入到数据库。

固定式输入insert的内容,但是来自于简单的xml文件中不能自己改,实实在在的说就是仍调用以前的插入方式,但是能用
后期会有采用表单输入的方式更加完善

	@RequestMapping("doInsert")
	public String doInsert() {
		//但是不行哦
		goodsDao.insert1();//但是只能插入一次还是调用固定的写法(GoodsMapper.xml),不然还需要添加输入框以及不知名的情况
		return "redirect:doGoodsUI";
	}

操作方式就是浏览器输入

遇到的问题sql.date和utill.date的区别

1) java.sql.Date是java.util.Date的子类,是一个包装了毫秒值的瘦包装器,允许 JDBC 将毫秒值标识为 SQL DATE 值。毫秒值表示自 1970 年 1 月 1 日 00:00:00 GMT 以来经过的毫秒数。 为了与 SQL DATE 的定义一致,由 java.sql.Date 实例包装的毫秒值必须通过将时间、分钟、秒和毫秒设置为与该实例相关的特定时区中的零来“规范化”。 说白了,java.sql.Date就是与数据库Date相对应的一个类型,而java.util.Date是纯java的Date。

2)JAVA里提供的日期和时间类,java.sql.Date和java.sql.Time,只会从数据库里读取某部分值,这有时会导致丢失数据。例如一个包含2002/05/22 5:00:57 PM的字段,读取日期时得到的是2002/05/22,而读取时间时得到的是5:00:57 PM. 你需要了解数据库里存储时间的精度。有些数据库,比如MySQL,精度为毫秒,然而另一些数据库,包括Oracle,存储SQL DATE类型数据时,毫秒部分的数据是不保存的。以下操作中容易出现不易被发现的BUG:获得一个JAVA里的日期对象。 从数据库里读取日期 试图比较两个日期对象是否相等。如果毫秒部分丢失,本来认为相等的两个日期对象用Equals方法可能返回false。.sql.Timestamp类比java.util.Date类精确度要高。这个类包了一个getTime()方法,但是它不会返回额外精度部分的数据,因此必须使用…

总之,java.util.Date 就是Java的日期对象,而java.sql.Date 是针对SQL语句使用的,只包含日期而没有时间部分。

2020/7/4–第七天–SpringBoot 综合实践实现

练习三:将页面用户输入的商品信息写入到数据库。
完善采用表单输入的方式

	@Insert("insert into tb_goods (name,remark,createdTime) values (#{name},#{remark},now())")
	int insertObject(Goods entity);

其他层的自己书写一下不难

    <form action="doSaveObject" method="post">
        <ul>
           <li>name:
           <li><input type="text" name="name">
           <li>remark:
           <li><textarea rows="5" cols="50" name="remark"></textarea>
           <li><input type="submit" value="save">
        </ul>
    </form>

上午主讲后端人员如何书写前端,借助bootstrap的css样式

https://start.spring.io/建包可以通过这个网站

下午有几个难点!!!!!!!!!!主要是在网页页面的一些书写

  1. 模板出错问题:html放错了地方
  2. @Mapper和@Service容易忘记添加:
  3. 删除传参出错:
  4. script–function的书写方式:
  5. jsp/js:

待做笔记——老师主要结合样式表加上前天说的内容结合,但是在html中因为样式的多样操作会有很多细节上的问题

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值