5/22 17:03

This commit is contained in:
zyx 2023-05-22 17:03:44 +08:00
parent 7f23a964c0
commit 2bcd1eb1f4
11 changed files with 653 additions and 0 deletions

View File

@ -219,6 +219,64 @@
</TableInfoDTO> </TableInfoDTO>
</value> </value>
</entry> </entry>
<entry key="person_info.tb_brand">
<value>
<TableInfoDTO>
<option name="fullColumn">
<list>
<ColumnInfoDTO>
<option name="custom" value="false" />
<option name="ext" value="{}" />
<option name="name" value="id" />
<option name="type" value="java.lang.Integer" />
</ColumnInfoDTO>
<ColumnInfoDTO>
<option name="custom" value="false" />
<option name="ext" value="{}" />
<option name="name" value="brandName" />
<option name="type" value="java.lang.String" />
</ColumnInfoDTO>
<ColumnInfoDTO>
<option name="custom" value="false" />
<option name="ext" value="{}" />
<option name="name" value="companyName" />
<option name="type" value="java.lang.String" />
</ColumnInfoDTO>
<ColumnInfoDTO>
<option name="custom" value="false" />
<option name="ext" value="{}" />
<option name="name" value="ordered" />
<option name="type" value="java.lang.Integer" />
</ColumnInfoDTO>
<ColumnInfoDTO>
<option name="custom" value="false" />
<option name="ext" value="{}" />
<option name="name" value="description" />
<option name="type" value="java.lang.String" />
</ColumnInfoDTO>
<ColumnInfoDTO>
<option name="custom" value="false" />
<option name="ext" value="{}" />
<option name="name" value="status" />
<option name="type" value="java.lang.Integer" />
</ColumnInfoDTO>
<ColumnInfoDTO>
<option name="custom" value="false" />
<option name="ext" value="{}" />
<option name="name" value="createDate" />
<option name="type" value="java.util.Date" />
</ColumnInfoDTO>
</list>
</option>
<option name="name" value="TbBrand" />
<option name="preName" value="" />
<option name="saveModelName" value="spring-mybatis01-522" />
<option name="savePackageName" value="cn.czyx007" />
<option name="savePath" value="./spring-mybatis01-522/src/main/java/cn/czyx007" />
<option name="templateGroupName" value="Default" />
</TableInfoDTO>
</value>
</entry>
<entry key="person_info.teacher"> <entry key="person_info.teacher">
<value> <value>
<TableInfoDTO> <TableInfoDTO>

View File

@ -12,6 +12,98 @@
<maven.compiler.source>11</maven.compiler.source> <maven.compiler.source>11</maven.compiler.source>
<maven.compiler.target>11</maven.compiler.target> <maven.compiler.target>11</maven.compiler.target>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding> <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<!-- spring版本号 RELEASE 正式版,经过测试更加稳定-->
<spring.version>5.0.3.RELEASE</spring.version>
<!--mybatis版本-->
<mybatis.version>3.5.6</mybatis.version>
<!--spring-mybatis-->
<mybatis-spring.version>1.3.1</mybatis-spring.version>
<!--mysql数据库驱动版本-->
<mysql.version>8.0.31</mysql.version>
<!--junit测试包版本号-->
<junit.version>4.13.1</junit.version>
<druid-version>1.1.10</druid-version>
</properties> </properties>
<dependencies>
<!--spring相关jdbc的操作依赖-->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-jdbc</artifactId>
<version>${spring.version}</version>
</dependency>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>${mysql.version}</version>
</dependency>
<dependency>
<groupId>org.mybatis</groupId>
<artifactId>mybatis</artifactId>
<version>${mybatis.version}</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context</artifactId>
<version>${spring.version}</version>
</dependency>
<!-- MyBatis/Spring -->
<dependency>
<groupId>org.mybatis</groupId>
<artifactId>mybatis-spring</artifactId>
<version>${mybatis-spring.version}</version>
</dependency>
<!--alibaba druid datasource-->
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>druid</artifactId>
<version>${druid-version}</version>
</dependency>
<!--spring的事务-->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-tx</artifactId>
<version>${spring.version}</version>
</dependency>
<!--切面配置-->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-aspects</artifactId>
<version>${spring.version}</version>
</dependency>
<dependency>
<groupId>com.github.pagehelper</groupId>
<artifactId>pagehelper</artifactId>
<version>5.3.2</version>
</dependency>
<dependency>
<groupId>com.github.jsqlparser</groupId>
<artifactId>jsqlparser</artifactId>
<version>0.9.5</version>
</dependency>
<!-- junit测试包 -->
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>${junit.version}</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<version>1.18.20</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-test</artifactId>
<version>${spring.version}</version>
</dependency>
</dependencies>
</project> </project>

View File

@ -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;
}

View File

@ -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<TbBrand> 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<TbBrand> 实例对象列表
* @return 影响行数
*/
int insertBatch(@Param("entities") List<TbBrand> entities);
/**
* 批量新增或按主键更新数据MyBatis原生foreach方法
*
* @param entities List<TbBrand> 实例对象列表
* @return 影响行数
* @throws org.springframework.jdbc.BadSqlGrammarException 入参是空List的时候会抛SQL语句错误的异常请自行校验入参
*/
int insertOrUpdateBatch(@Param("entities") List<TbBrand> entities);
/**
* 修改数据
*
* @param tbBrand 实例对象
* @return 影响行数
*/
int update(TbBrand tbBrand);
/**
* 通过主键删除数据
*
* @param id 主键
* @return 影响行数
*/
int deleteById(Integer id);
}

View File

@ -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);
}

View File

@ -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;
}
}

View File

@ -0,0 +1,153 @@
<?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="cn.czyx007.dao.TbBrandDao">
<resultMap type="cn.czyx007.bean.TbBrand" id="TbBrandMap">
<result property="id" column="id" jdbcType="INTEGER"/>
<result property="brandName" column="brand_name" jdbcType="VARCHAR"/>
<result property="companyName" column="company_name" jdbcType="VARCHAR"/>
<result property="ordered" column="ordered" jdbcType="INTEGER"/>
<result property="description" column="description" jdbcType="VARCHAR"/>
<result property="status" column="status" jdbcType="INTEGER"/>
<result property="createDate" column="create_date" jdbcType="TIMESTAMP"/>
</resultMap>
<!--查询单个-->
<select id="queryById" resultMap="TbBrandMap">
select id,
brand_name,
company_name,
ordered,
description,
status,
create_date
from person_info.tb_brand
where id = #{id}
</select>
<!--查询指定行数据-->
<select id="queryAllByLimit" resultMap="TbBrandMap">
select
id, brand_name, company_name, ordered, description, status, create_date
from person_info.tb_brand
<where>
<if test="id != null">
and id = #{id}
</if>
<if test="brandName != null and brandName != ''">
and brand_name = #{brandName}
</if>
<if test="companyName != null and companyName != ''">
and company_name = #{companyName}
</if>
<if test="ordered != null">
and ordered = #{ordered}
</if>
<if test="description != null and description != ''">
and description = #{description}
</if>
<if test="status != null">
and status = #{status}
</if>
<if test="createDate != null">
and create_date = #{createDate}
</if>
</where>
limit #{pageable.offset}, #{pageable.pageSize}
</select>
<!--统计总行数-->
<select id="count" resultType="java.lang.Long">
select count(1)
from person_info.tb_brand
<where>
<if test="id != null">
and id = #{id}
</if>
<if test="brandName != null and brandName != ''">
and brand_name = #{brandName}
</if>
<if test="companyName != null and companyName != ''">
and company_name = #{companyName}
</if>
<if test="ordered != null">
and ordered = #{ordered}
</if>
<if test="description != null and description != ''">
and description = #{description}
</if>
<if test="status != null">
and status = #{status}
</if>
<if test="createDate != null">
and create_date = #{createDate}
</if>
</where>
</select>
<!--新增所有列-->
<insert id="insert" keyProperty="id" useGeneratedKeys="true">
insert into person_info.tb_brand(brand_name, company_name, ordered, description, status, create_date)
values (#{brandName}, #{companyName}, #{ordered}, #{description}, #{status}, #{createDate})
</insert>
<insert id="insertBatch" keyProperty="id" useGeneratedKeys="true">
insert into person_info.tb_brand(brand_name, company_name, ordered, description, status, create_date)
values
<foreach collection="entities" item="entity" separator=",">
(#{entity.brandName}, #{entity.companyName}, #{entity.ordered}, #{entity.description}, #{entity.status},
#{entity.createDate})
</foreach>
</insert>
<insert id="insertOrUpdateBatch" keyProperty="id" useGeneratedKeys="true">
insert into person_info.tb_brand(brand_name, company_name, ordered, description, status, create_date)
values
<foreach collection="entities" item="entity" separator=",">
(#{entity.brandName}, #{entity.companyName}, #{entity.ordered}, #{entity.description}, #{entity.status},
#{entity.createDate})
</foreach>
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)
</insert>
<!--通过主键修改数据-->
<update id="update">
update person_info.tb_brand
<set>
<if test="brandName != null and brandName != ''">
brand_name = #{brandName},
</if>
<if test="companyName != null and companyName != ''">
company_name = #{companyName},
</if>
<if test="ordered != null">
ordered = #{ordered},
</if>
<if test="description != null and description != ''">
description = #{description},
</if>
<if test="status != null">
status = #{status},
</if>
<if test="createDate != null">
create_date = #{createDate},
</if>
</set>
where id = #{id}
</update>
<!--通过主键删除-->
<delete id="deleteById">
delete
from person_info.tb_brand
where id = #{id}
</delete>
</mapper>

View File

@ -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

View File

@ -0,0 +1,52 @@
<?xml version="1.0" encoding="utf-8" ?>
<!DOCTYPE configuration
PUBLIC "-//mybatis.org//DTD Config 3.0//EN"
"http://mybatis.org/dtd/mybatis-3-config.dtd">
<configuration>
<!--
标签顺序必须是:
properties?,settings?,typeAliases?,
typeHandlers?,objectFactory?,
objectWrapperFactory?,reflectorFactory?,
plugins?,environments?,
databaseIdProvider?,mappers?
-->
<!-- <properties resource="jdbc.properties"/>-->
<settings>
<setting name="logImpl" value="STDOUT_LOGGING"/>
<setting name="autoMappingBehavior" value="FULL"/>
<!--将下划线映射为驼峰-->
<setting name="mapUnderscoreToCamelCase" value="true"/>
<setting name="lazyLoadingEnabled" value="true"/>
<setting name="cacheEnabled" value="true"/>
</settings>
<typeAliases>
<package name="cn.czyx007.bean"/>
</typeAliases>
<plugins>
<plugin interceptor="com.github.pagehelper.PageInterceptor">
<!-- 启用合理化,防止第一页再点击上一页,最后一页再点击下一页 -->
<property name="reasonable" value="true"/>
</plugin>
</plugins>
<!-- <environments default="development">-->
<!-- <environment id="development">-->
<!-- <transactionManager type="JDBC"/>-->
<!-- <dataSource type="POOLED">-->
<!-- <property name="driver" value="${jdbc.driver}"/>-->
<!-- <property name="url" value="${jdbc.url}"/>-->
<!-- <property name="username" value="${jdbc.username}"/>-->
<!-- <property name="password" value="${jdbc.password}"/>-->
<!-- </dataSource>-->
<!-- </environment>-->
<!-- </environments>-->
<mappers>
<package name="cn.czyx007.dao"/>
</mappers>
</configuration>

View File

@ -0,0 +1,41 @@
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context.xsd">
<context:component-scan base-package="cn.czyx007.*"/>
<bean class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
<property name="location" value="classpath:jdbc.properties"/>
</bean>
<bean id="druidDataSource" class="com.alibaba.druid.pool.DruidDataSource">
<property name="driverClassName" value="${jdbc.driver}"/>
<property name="url" value="${jdbc.url}"/>
<property name="username" value="${jdbc.username}"/>
<property name="password" value="${jdbc.password}"/>
<!-- 配置初始化大小、最小、最大 -->
<property name="initialSize" value="1" />
<property name="minIdle" value="1" />
<property name="maxActive" value="20" />
<!-- 配置获取连接等待超时的时间 -->
<property name="maxWait" value="60000" />
<!-- 配置间隔多久才进行一次检测,检测需要关闭的空闲连接,单位是毫秒 -->
<property name="timeBetweenEvictionRunsMillis" value="60000" />
<!-- 配置一个连接在池中最小生存的时间,单位是毫秒 -->
<property name="minEvictableIdleTimeMillis" value="300000" />
</bean>
<bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
<property name="dataSource" ref="druidDataSource"/>
<property name="configLocation" value="classpath:mybatis-config.xml"/>
</bean>
<bean id="maperScannerConfigurer" class="org.mybatis.spring.mapper.MapperScannerConfigurer">
<property name="basePackage" value="cn.czyx007.dao"/>
</bean>
</beans>

View File

@ -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);
}
}