From 2bcd1eb1f41867dd398439d4330c16b6ed4ee19a Mon Sep 17 00:00:00 2001 From: zyx <1029606625@qq.com> Date: Mon, 22 May 2023 17:03:44 +0800 Subject: [PATCH] 5/22 17:03 --- .idea/easyCodeTableSetting.xml | 58 +++++++ spring-mybatis01-522/pom.xml | 92 +++++++++++ .../main/java/cn/czyx007/bean/TbBrand.java | 35 ++++ .../main/java/cn/czyx007/dao/TbBrandDao.java | 84 ++++++++++ .../cn/czyx007/service/TbBrandService.java | 45 ++++++ .../service/impl/TbBrandServiceImpl.java | 65 ++++++++ .../resources/cn/czyx007/dao/TbBrandDao.xml | 153 ++++++++++++++++++ .../src/main/resources/jdbc.properties | 4 + .../src/main/resources/mybatis-config.xml | 52 ++++++ .../src/main/resources/spring.xml | 41 +++++ .../src/test/java/TestDemo.java | 24 +++ 11 files changed, 653 insertions(+) create mode 100644 spring-mybatis01-522/src/main/java/cn/czyx007/bean/TbBrand.java create mode 100644 spring-mybatis01-522/src/main/java/cn/czyx007/dao/TbBrandDao.java create mode 100644 spring-mybatis01-522/src/main/java/cn/czyx007/service/TbBrandService.java create mode 100644 spring-mybatis01-522/src/main/java/cn/czyx007/service/impl/TbBrandServiceImpl.java create mode 100644 spring-mybatis01-522/src/main/resources/cn/czyx007/dao/TbBrandDao.xml create mode 100644 spring-mybatis01-522/src/main/resources/jdbc.properties create mode 100644 spring-mybatis01-522/src/main/resources/mybatis-config.xml create mode 100644 spring-mybatis01-522/src/main/resources/spring.xml create mode 100644 spring-mybatis01-522/src/test/java/TestDemo.java diff --git a/.idea/easyCodeTableSetting.xml b/.idea/easyCodeTableSetting.xml index 70a2917..ae9b630 100644 --- a/.idea/easyCodeTableSetting.xml +++ b/.idea/easyCodeTableSetting.xml @@ -219,6 +219,64 @@ + + + + + + + diff --git a/spring-mybatis01-522/pom.xml b/spring-mybatis01-522/pom.xml index faf4483..b31d319 100644 --- a/spring-mybatis01-522/pom.xml +++ b/spring-mybatis01-522/pom.xml @@ -12,6 +12,98 @@ 11 11 UTF-8 + + + 5.0.3.RELEASE + + 3.5.6 + + 1.3.1 + + 8.0.31 + + 4.13.1 + 1.1.10 + + + + org.springframework + spring-jdbc + ${spring.version} + + + mysql + mysql-connector-java + ${mysql.version} + + + org.mybatis + mybatis + ${mybatis.version} + + + org.springframework + spring-context + ${spring.version} + + + + + org.mybatis + mybatis-spring + ${mybatis-spring.version} + + + + com.alibaba + druid + ${druid-version} + + + + + + org.springframework + spring-tx + ${spring.version} + + + + org.springframework + spring-aspects + ${spring.version} + + + + com.github.pagehelper + pagehelper + 5.3.2 + + + com.github.jsqlparser + jsqlparser + 0.9.5 + + + + + junit + junit + ${junit.version} + test + + + org.projectlombok + lombok + 1.18.20 + + + org.springframework + spring-test + ${spring.version} + + + \ No newline at end of file diff --git a/spring-mybatis01-522/src/main/java/cn/czyx007/bean/TbBrand.java b/spring-mybatis01-522/src/main/java/cn/czyx007/bean/TbBrand.java new file mode 100644 index 0000000..f245be9 --- /dev/null +++ b/spring-mybatis01-522/src/main/java/cn/czyx007/bean/TbBrand.java @@ -0,0 +1,35 @@ +package cn.czyx007.bean; + +import lombok.AllArgsConstructor; +import lombok.Data; +import lombok.NoArgsConstructor; + +import java.io.Serializable; +import java.util.Date; + +/** + * (TbBrand)实体类 + * + * @author 张宇轩 + * @since 2023-05-22 15:33:51 + */ +@Data +@NoArgsConstructor +@AllArgsConstructor +public class TbBrand implements Serializable { + private static final long serialVersionUID = 636975416687068838L; + + private Integer id; + + private String brandName; + + private String companyName; + + private Integer ordered; + + private String description; + + private Integer status; + + private Date createDate; +} diff --git a/spring-mybatis01-522/src/main/java/cn/czyx007/dao/TbBrandDao.java b/spring-mybatis01-522/src/main/java/cn/czyx007/dao/TbBrandDao.java new file mode 100644 index 0000000..8dffd02 --- /dev/null +++ b/spring-mybatis01-522/src/main/java/cn/czyx007/dao/TbBrandDao.java @@ -0,0 +1,84 @@ +package cn.czyx007.dao; + +import cn.czyx007.bean.TbBrand; +import org.apache.ibatis.annotations.Param; + +import java.awt.print.Pageable; +import java.util.List; + +/** + * (TbBrand)表数据库访问层 + * + * @author 张宇轩 + * @since 2023-05-22 15:33:50 + */ +public interface TbBrandDao { + + /** + * 通过ID查询单条数据 + * + * @param id 主键 + * @return 实例对象 + */ + TbBrand queryById(Integer id); + + /** + * 查询指定行数据 + * + * @param tbBrand 查询条件 + * @param pageable 分页对象 + * @return 对象列表 + */ + List queryAllByLimit(TbBrand tbBrand, @Param("pageable") Pageable pageable); + + /** + * 统计总行数 + * + * @param tbBrand 查询条件 + * @return 总行数 + */ + long count(TbBrand tbBrand); + + /** + * 新增数据 + * + * @param tbBrand 实例对象 + * @return 影响行数 + */ + int insert(TbBrand tbBrand); + + /** + * 批量新增数据(MyBatis原生foreach方法) + * + * @param entities List 实例对象列表 + * @return 影响行数 + */ + int insertBatch(@Param("entities") List entities); + + /** + * 批量新增或按主键更新数据(MyBatis原生foreach方法) + * + * @param entities List 实例对象列表 + * @return 影响行数 + * @throws org.springframework.jdbc.BadSqlGrammarException 入参是空List的时候会抛SQL语句错误的异常,请自行校验入参 + */ + int insertOrUpdateBatch(@Param("entities") List entities); + + /** + * 修改数据 + * + * @param tbBrand 实例对象 + * @return 影响行数 + */ + int update(TbBrand tbBrand); + + /** + * 通过主键删除数据 + * + * @param id 主键 + * @return 影响行数 + */ + int deleteById(Integer id); + +} + diff --git a/spring-mybatis01-522/src/main/java/cn/czyx007/service/TbBrandService.java b/spring-mybatis01-522/src/main/java/cn/czyx007/service/TbBrandService.java new file mode 100644 index 0000000..a78ba2f --- /dev/null +++ b/spring-mybatis01-522/src/main/java/cn/czyx007/service/TbBrandService.java @@ -0,0 +1,45 @@ +package cn.czyx007.service; + +import cn.czyx007.bean.TbBrand; + +/** + * (TbBrand)表服务接口 + * + * @author 张宇轩 + * @since 2023-05-22 15:33:50 + */ +public interface TbBrandService { + + /** + * 通过ID查询单条数据 + * + * @param id 主键 + * @return 实例对象 + */ + TbBrand queryById(Integer id); + + /** + * 新增数据 + * + * @param tbBrand 实例对象 + * @return 实例对象 + */ + TbBrand insert(TbBrand tbBrand); + + /** + * 修改数据 + * + * @param tbBrand 实例对象 + * @return 实例对象 + */ + TbBrand update(TbBrand tbBrand); + + /** + * 通过主键删除数据 + * + * @param id 主键 + * @return 是否成功 + */ + boolean deleteById(Integer id); + +} diff --git a/spring-mybatis01-522/src/main/java/cn/czyx007/service/impl/TbBrandServiceImpl.java b/spring-mybatis01-522/src/main/java/cn/czyx007/service/impl/TbBrandServiceImpl.java new file mode 100644 index 0000000..44bc4a0 --- /dev/null +++ b/spring-mybatis01-522/src/main/java/cn/czyx007/service/impl/TbBrandServiceImpl.java @@ -0,0 +1,65 @@ +package cn.czyx007.service.impl; + +import cn.czyx007.bean.TbBrand; +import cn.czyx007.dao.TbBrandDao; +import cn.czyx007.service.TbBrandService; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.stereotype.Service; + +/** + * (TbBrand)表服务实现类 + * + * @author 张宇轩 + * @since 2023-05-22 15:33:50 + */ +@Service +public class TbBrandServiceImpl implements TbBrandService { + @Autowired + TbBrandDao tbBrandDao; + + /** + * 通过ID查询单条数据 + * + * @param id 主键 + * @return 实例对象 + */ + @Override + public TbBrand queryById(Integer id) { + return tbBrandDao.queryById(id); + } + + /** + * 新增数据 + * + * @param tbBrand 实例对象 + * @return 实例对象 + */ + @Override + public TbBrand insert(TbBrand tbBrand) { + this.tbBrandDao.insert(tbBrand); + return tbBrand; + } + + /** + * 修改数据 + * + * @param tbBrand 实例对象 + * @return 实例对象 + */ + @Override + public TbBrand update(TbBrand tbBrand) { + this.tbBrandDao.update(tbBrand); + return this.queryById(tbBrand.getId()); + } + + /** + * 通过主键删除数据 + * + * @param id 主键 + * @return 是否成功 + */ + @Override + public boolean deleteById(Integer id) { + return this.tbBrandDao.deleteById(id) > 0; + } +} diff --git a/spring-mybatis01-522/src/main/resources/cn/czyx007/dao/TbBrandDao.xml b/spring-mybatis01-522/src/main/resources/cn/czyx007/dao/TbBrandDao.xml new file mode 100644 index 0000000..7f3988e --- /dev/null +++ b/spring-mybatis01-522/src/main/resources/cn/czyx007/dao/TbBrandDao.xml @@ -0,0 +1,153 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + insert into person_info.tb_brand(brand_name, company_name, ordered, description, status, create_date) + values (#{brandName}, #{companyName}, #{ordered}, #{description}, #{status}, #{createDate}) + + + + insert into person_info.tb_brand(brand_name, company_name, ordered, description, status, create_date) + values + + (#{entity.brandName}, #{entity.companyName}, #{entity.ordered}, #{entity.description}, #{entity.status}, + #{entity.createDate}) + + + + + insert into person_info.tb_brand(brand_name, company_name, ordered, description, status, create_date) + values + + (#{entity.brandName}, #{entity.companyName}, #{entity.ordered}, #{entity.description}, #{entity.status}, + #{entity.createDate}) + + on duplicate key update + brand_name = values(brand_name), + company_name = values(company_name), + ordered = values(ordered), + description = values(description), + status = values(status), + create_date = values(create_date) + + + + + update person_info.tb_brand + + + brand_name = #{brandName}, + + + company_name = #{companyName}, + + + ordered = #{ordered}, + + + description = #{description}, + + + status = #{status}, + + + create_date = #{createDate}, + + + where id = #{id} + + + + + delete + from person_info.tb_brand + where id = #{id} + + + + diff --git a/spring-mybatis01-522/src/main/resources/jdbc.properties b/spring-mybatis01-522/src/main/resources/jdbc.properties new file mode 100644 index 0000000..0058fcd --- /dev/null +++ b/spring-mybatis01-522/src/main/resources/jdbc.properties @@ -0,0 +1,4 @@ +jdbc.driver=com.mysql.cj.jdbc.Driver +jdbc.url=jdbc:mysql://localhost:3306/person_info?serverTimezone=Asia/Shanghai +jdbc.username=root +jdbc.password=zyx007 \ No newline at end of file diff --git a/spring-mybatis01-522/src/main/resources/mybatis-config.xml b/spring-mybatis01-522/src/main/resources/mybatis-config.xml new file mode 100644 index 0000000..ce97246 --- /dev/null +++ b/spring-mybatis01-522/src/main/resources/mybatis-config.xml @@ -0,0 +1,52 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + \ No newline at end of file diff --git a/spring-mybatis01-522/src/main/resources/spring.xml b/spring-mybatis01-522/src/main/resources/spring.xml new file mode 100644 index 0000000..35ce9ec --- /dev/null +++ b/spring-mybatis01-522/src/main/resources/spring.xml @@ -0,0 +1,41 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + diff --git a/spring-mybatis01-522/src/test/java/TestDemo.java b/spring-mybatis01-522/src/test/java/TestDemo.java new file mode 100644 index 0000000..b579131 --- /dev/null +++ b/spring-mybatis01-522/src/test/java/TestDemo.java @@ -0,0 +1,24 @@ +import cn.czyx007.bean.TbBrand; +import cn.czyx007.service.TbBrandService; +import org.junit.Test; +import org.junit.runner.RunWith; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.test.context.ContextConfiguration; +import org.springframework.test.context.junit4.SpringJUnit4ClassRunner; + +/** + * @author : 张宇轩 + * @createTime : 2023/5/22 - 16:02 + */ +@ContextConfiguration(locations = {"classpath:spring.xml"}) +@RunWith(value = SpringJUnit4ClassRunner.class) +public class TestDemo { + @Autowired + TbBrandService brandService; + + @Test + public void test1(){ + TbBrand tbBrand = brandService.queryById(5); + System.out.println("tbBrand = " + tbBrand); + } +}