5/19 10:56
This commit is contained in:
parent
9851096082
commit
1f710543bc
1
.idea/compiler.xml
generated
1
.idea/compiler.xml
generated
@ -11,6 +11,7 @@
|
||||
<module name="demo516" />
|
||||
<module name="demo515" />
|
||||
<module name="mybatis517-518" />
|
||||
<module name="mybatis519" />
|
||||
</profile>
|
||||
</annotationProcessing>
|
||||
</component>
|
||||
|
2
.idea/encodings.xml
generated
2
.idea/encodings.xml
generated
@ -9,5 +9,7 @@
|
||||
<file url="file://$PROJECT_DIR$/mybatis516/src/main/resources" charset="UTF-8" />
|
||||
<file url="file://$PROJECT_DIR$/mybatis517-518/src/main/java" charset="UTF-8" />
|
||||
<file url="file://$PROJECT_DIR$/mybatis517-518/src/main/resources" charset="UTF-8" />
|
||||
<file url="file://$PROJECT_DIR$/mybatis519/src/main/java" charset="UTF-8" />
|
||||
<file url="file://$PROJECT_DIR$/mybatis519/src/main/resources" charset="UTF-8" />
|
||||
</component>
|
||||
</project>
|
1
.idea/misc.xml
generated
1
.idea/misc.xml
generated
@ -12,6 +12,7 @@
|
||||
<option value="$PROJECT_DIR$/mybatis516/pom.xml" />
|
||||
<option value="$PROJECT_DIR$/mybatis517/pom.xml" />
|
||||
<option value="$PROJECT_DIR$/mybatis517-518/pom.xml" />
|
||||
<option value="$PROJECT_DIR$/mybatis519/pom.xml" />
|
||||
</list>
|
||||
</option>
|
||||
</component>
|
||||
|
51
mybatis519/pom.xml
Normal file
51
mybatis519/pom.xml
Normal file
@ -0,0 +1,51 @@
|
||||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<project xmlns="http://maven.apache.org/POM/4.0.0"
|
||||
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
|
||||
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
|
||||
<modelVersion>4.0.0</modelVersion>
|
||||
|
||||
<groupId>cn.czyx007</groupId>
|
||||
<artifactId>mybatis519</artifactId>
|
||||
<version>1.0-SNAPSHOT</version>
|
||||
|
||||
<properties>
|
||||
<maven.compiler.source>11</maven.compiler.source>
|
||||
<maven.compiler.target>11</maven.compiler.target>
|
||||
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
|
||||
</properties>
|
||||
|
||||
<dependencies>
|
||||
<dependency>
|
||||
<groupId>mysql</groupId>
|
||||
<artifactId>mysql-connector-java</artifactId>
|
||||
<version>8.0.31</version>
|
||||
<scope>runtime</scope>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>org.projectlombok</groupId>
|
||||
<artifactId>lombok</artifactId>
|
||||
<version>1.18.20</version>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>org.mybatis</groupId>
|
||||
<artifactId>mybatis</artifactId>
|
||||
<version>3.5.10</version>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>junit</groupId>
|
||||
<artifactId>junit</artifactId>
|
||||
<version>4.13.2</version>
|
||||
<scope>test</scope>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>log4j</groupId>
|
||||
<artifactId>log4j</artifactId>
|
||||
<version>1.2.17</version>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>com.github.pagehelper</groupId>
|
||||
<artifactId>pagehelper</artifactId>
|
||||
<version>5.2.0</version>
|
||||
</dependency>
|
||||
</dependencies>
|
||||
</project>
|
51
mybatis519/src/main/java/cn/czyx007/bean/Computers.java
Normal file
51
mybatis519/src/main/java/cn/czyx007/bean/Computers.java
Normal file
@ -0,0 +1,51 @@
|
||||
package cn.czyx007.bean;
|
||||
|
||||
import lombok.AllArgsConstructor;
|
||||
import lombok.Data;
|
||||
import lombok.NoArgsConstructor;
|
||||
|
||||
import java.io.Serializable;
|
||||
|
||||
/**
|
||||
* (Computers)实体类
|
||||
*
|
||||
* @author 张宇轩
|
||||
* @since 2023-05-18 14:50:06
|
||||
*/
|
||||
@Data
|
||||
@NoArgsConstructor
|
||||
@AllArgsConstructor
|
||||
public class Computers implements Serializable {
|
||||
private static final long serialVersionUID = 943332959360147693L;
|
||||
/**
|
||||
* 主键
|
||||
*/
|
||||
private Long id;
|
||||
/**
|
||||
* 名牌
|
||||
*/
|
||||
private String brand;
|
||||
/**
|
||||
* 价格
|
||||
*/
|
||||
private Float price;
|
||||
/**
|
||||
* 内存
|
||||
*/
|
||||
private Float runmem;
|
||||
/**
|
||||
* cpu类型
|
||||
*/
|
||||
private String cpu;
|
||||
/**
|
||||
* 显卡
|
||||
*/
|
||||
private Float xianka;
|
||||
/**
|
||||
* 1 显示 0隐藏
|
||||
*/
|
||||
private String status;
|
||||
|
||||
private Float startPrice;
|
||||
private Float endPrice;
|
||||
}
|
@ -0,0 +1,46 @@
|
||||
package cn.czyx007.mapper;
|
||||
|
||||
import cn.czyx007.bean.Computers;
|
||||
import org.apache.ibatis.annotations.Select;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
/**
|
||||
* @author : 张宇轩
|
||||
* @createTime : 2023/5/18 - 14:55
|
||||
*/
|
||||
public interface ComputersMapper {
|
||||
List<Computers> getComputers();
|
||||
|
||||
@Select("<script>" +
|
||||
"select * from person_info.computers\n" +
|
||||
" <where>\n" +
|
||||
" <if test=\"brand!=null and brand != ''\">\n" +
|
||||
" and brand like concat('%',#{brand},'%')\n" +
|
||||
" </if>\n" +
|
||||
" <if test=\"startPrice!=null\">\n" +
|
||||
" and price >= #{startPrice}\n" +
|
||||
" </if>\n" +
|
||||
" <if test=\"endPrice!=null\">\n" +
|
||||
" and price <= #{endPrice}\n" +
|
||||
" </if>\n" +
|
||||
" <if test=\"runmem!=null\">\n" +
|
||||
" and runmem = #{runmem}\n" +
|
||||
" </if>\n" +
|
||||
" <if test=\"cpu!=null and cpu!=''\">\n" +
|
||||
" and cpu = #{cpu}\n" +
|
||||
" </if>\n" +
|
||||
" <if test=\"xianka!=null\">\n" +
|
||||
" and xianka = #{xianka}\n" +
|
||||
" </if>\n" +
|
||||
" </where>" +
|
||||
"</script>")
|
||||
List<Computers> queryByConditionIf(Computers computers);
|
||||
|
||||
int updateByConditionSet(Computers computers);
|
||||
List<Computers> queryByConditionChoose(Computers computers);
|
||||
List<Computers> queryByConditionTrim(Computers computers);
|
||||
int updateByConditionTrim(Computers computers);
|
||||
|
||||
List<Computers> queryByForEach(long[] ids);
|
||||
}
|
@ -0,0 +1,32 @@
|
||||
package cn.czyx007.utils;
|
||||
|
||||
import org.apache.ibatis.io.Resources;
|
||||
import org.apache.ibatis.session.SqlSession;
|
||||
import org.apache.ibatis.session.SqlSessionFactory;
|
||||
import org.apache.ibatis.session.SqlSessionFactoryBuilder;
|
||||
|
||||
import java.io.IOException;
|
||||
import java.io.InputStream;
|
||||
|
||||
/**
|
||||
* @author : 张宇轩
|
||||
* @createTime : 2023/5/16 - 14:08
|
||||
*/
|
||||
public class SqlSessionUtil {
|
||||
public static SqlSession getSqlSession(){
|
||||
SqlSession sqlSession = null;
|
||||
InputStream is;
|
||||
try {
|
||||
//1.获取核心配置文件的输入流
|
||||
is = Resources.getResourceAsStream("mybatis-config.xml");
|
||||
//2.获取SqlSessionFactoryBuilder对象
|
||||
SqlSessionFactoryBuilder sqlSessionFactoryBuilder = new SqlSessionFactoryBuilder();
|
||||
//3.获取SqlSessionFactory对象
|
||||
SqlSessionFactory sqlSessionFactory = sqlSessionFactoryBuilder.build(is);
|
||||
sqlSession = sqlSessionFactory.openSession(true);
|
||||
} catch (IOException e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
return sqlSession;
|
||||
}
|
||||
}
|
@ -0,0 +1,101 @@
|
||||
<?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.mapper.ComputersMapper">
|
||||
|
||||
<update id="updateByConditionSet">
|
||||
update person_info.computers
|
||||
<set>
|
||||
<if test="price!=null">
|
||||
price = #{price},
|
||||
</if>
|
||||
<if test="xianka!=null">
|
||||
xianka = #{xianka}
|
||||
</if>
|
||||
</set>
|
||||
where id = #{id}
|
||||
</update>
|
||||
<update id="updateByConditionTrim">
|
||||
update person_info.computers
|
||||
<trim prefix="set" suffixOverrides=",">
|
||||
<if test="price!=null">
|
||||
price = #{price},
|
||||
</if>
|
||||
<if test="xianka!=null">
|
||||
xianka = #{xianka}
|
||||
</if>
|
||||
</trim>
|
||||
where id = #{id}
|
||||
</update>
|
||||
|
||||
<sql id="computerSql">
|
||||
id,brand,price,cpu,xianka
|
||||
</sql>
|
||||
<select id="getComputers" resultType="cn.czyx007.bean.Computers">
|
||||
select <include refid="computerSql"/>from person_info.computers
|
||||
</select>
|
||||
<!-- <select id="queryByConditionIf" resultType="cn.czyx007.bean.Computers">-->
|
||||
<!-- select * from person_info.computers-->
|
||||
<!-- <where>-->
|
||||
<!-- <if test="brand!=null and brand != ''">-->
|
||||
<!-- and brand like concat('%',#{brand},'%')-->
|
||||
<!-- </if>-->
|
||||
<!-- <if test="startPrice!=null">-->
|
||||
<!-- and price >= #{startPrice}-->
|
||||
<!-- </if>-->
|
||||
<!-- <if test="endPrice!=null">-->
|
||||
<!-- and price <= #{endPrice}-->
|
||||
<!-- </if>-->
|
||||
<!-- <if test="runmem!=null">-->
|
||||
<!-- and runmem = #{runmem}-->
|
||||
<!-- </if>-->
|
||||
<!-- <if test="cpu!=null and cpu!=''">-->
|
||||
<!-- and cpu = #{cpu}-->
|
||||
<!-- </if>-->
|
||||
<!-- <if test="xianka!=null">-->
|
||||
<!-- and xianka = #{xianka}-->
|
||||
<!-- </if>-->
|
||||
<!-- </where>-->
|
||||
<!-- </select>-->
|
||||
<select id="queryByConditionChoose" resultType="cn.czyx007.bean.Computers">
|
||||
SELECT * FROM person_info.computers
|
||||
<where>
|
||||
<choose>
|
||||
<when test="brand!=null and brand!=''">
|
||||
and brand like concat('%',#{brand},'%')
|
||||
</when>
|
||||
<when test="startPrice!=null">
|
||||
and price >= #{startPrice}
|
||||
</when>
|
||||
<when test="endPrice!=null">
|
||||
and price <= #{endPrice}
|
||||
</when>
|
||||
</choose>
|
||||
</where>
|
||||
order by id desc
|
||||
</select>
|
||||
<select id="queryByConditionTrim" resultType="cn.czyx007.bean.Computers">
|
||||
select * from person_info.computers
|
||||
<trim prefix="where" prefixOverrides="and">
|
||||
<if test="brand!=null and brand!=''">
|
||||
and brand like concat('%',#{brand},'%')
|
||||
</if>
|
||||
<if test="startPrice!=null">
|
||||
and price >= #{startPrice}
|
||||
</if>
|
||||
<if test="endPrice!=null">
|
||||
and price <= #{endPrice}
|
||||
</if>
|
||||
</trim>
|
||||
</select>
|
||||
<select id="queryByForEach" resultType="cn.czyx007.bean.Computers">
|
||||
select *
|
||||
from person_info.computers
|
||||
where id in
|
||||
<foreach collection="array" open="(" separator="," close=")" item="id">
|
||||
#{id}
|
||||
</foreach>
|
||||
</select>
|
||||
</mapper>
|
4
mybatis519/src/main/resources/jdbc.properties
Normal file
4
mybatis519/src/main/resources/jdbc.properties
Normal 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
|
21
mybatis519/src/main/resources/log4j.xml
Normal file
21
mybatis519/src/main/resources/log4j.xml
Normal file
@ -0,0 +1,21 @@
|
||||
<?xml version="1.0" encoding="UTF-8" ?>
|
||||
<!DOCTYPE log4j:configuration SYSTEM "log4j.dtd">
|
||||
<log4j:configuration xmlns:log4j="http://jakarta.apache.org/log4j/">
|
||||
<appender name="STDOUT" class="org.apache.log4j.ConsoleAppender">
|
||||
<param name="Encoding" value="UTF-8"/>
|
||||
<layout class="org.apache.log4j.PatternLayout">
|
||||
<param name="ConversionPattern" value="%-5p %d{MM-dd HH:mm:ss,SSS}
|
||||
%m (%F:%L) \n"/>
|
||||
</layout>
|
||||
</appender>
|
||||
<logger name="java.sql">
|
||||
<level value="debug"/>
|
||||
</logger>
|
||||
<logger name="org.apache.ibatis">
|
||||
<level value="info"/>
|
||||
</logger>
|
||||
<root>
|
||||
<level value="debug"/>
|
||||
<appender-ref ref="STDOUT"/>
|
||||
</root>
|
||||
</log4j:configuration>
|
50
mybatis519/src/main/resources/mybatis-config.xml
Normal file
50
mybatis519/src/main/resources/mybatis-config.xml
Normal file
@ -0,0 +1,50 @@
|
||||
<?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"/>
|
||||
</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.mapper"/>
|
||||
</mappers>
|
||||
</configuration>
|
43
mybatis519/src/test/java/TestComputers.java
Normal file
43
mybatis519/src/test/java/TestComputers.java
Normal file
@ -0,0 +1,43 @@
|
||||
import cn.czyx007.bean.Brand;
|
||||
import cn.czyx007.bean.Computers;
|
||||
import cn.czyx007.mapper.BrandMapper;
|
||||
import cn.czyx007.mapper.ComputersMapper;
|
||||
import cn.czyx007.utils.SqlSessionUtil;
|
||||
import com.github.pagehelper.PageInfo;
|
||||
import org.junit.Test;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
/**
|
||||
* @author : 张宇轩
|
||||
* @createTime : 2023/5/19 - 8:53
|
||||
*/
|
||||
public class TestComputers {
|
||||
@Test
|
||||
public void test1(){
|
||||
ComputersMapper mapper = SqlSessionUtil.getSqlSession().getMapper(ComputersMapper.class);
|
||||
mapper.queryByForEach(new long[]{1,2}).forEach(System.out::println);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void test2(){
|
||||
int pageNum=1;
|
||||
int pageSize=3;
|
||||
com.github.pagehelper.PageHelper.startPage(pageNum, pageSize);
|
||||
BrandMapper brandMapper = SqlSessionUtil.getSqlSession().getMapper(BrandMapper.class);
|
||||
List<Brand> brandList = brandMapper.getBrandAll();
|
||||
PageInfo<Brand> pageInfo = new PageInfo<>(brandList);
|
||||
System.out.println("总页数: " + pageInfo.getPages());
|
||||
System.out.println("总条数: " + pageInfo.getTotal());
|
||||
System.out.println("分页数据: " + pageInfo.getList());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testQueryByConditionIf(){
|
||||
ComputersMapper mapper = SqlSessionUtil.getSqlSession().getMapper(ComputersMapper.class);
|
||||
Computers computers = new Computers();
|
||||
computers.setBrand("联想");
|
||||
computers.setStartPrice(5000F);
|
||||
mapper.queryByConditionIf(computers).forEach(System.out::println);
|
||||
}
|
||||
}
|
15
mybatis519/src/test/java/TestWife.java
Normal file
15
mybatis519/src/test/java/TestWife.java
Normal file
@ -0,0 +1,15 @@
|
||||
import cn.czyx007.mapper.WifeMapper;
|
||||
import cn.czyx007.utils.SqlSessionUtil;
|
||||
import org.junit.Test;
|
||||
|
||||
/**
|
||||
* @author : 张宇轩
|
||||
* @createTime : 2023/5/19 - 10:34
|
||||
*/
|
||||
public class TestWife {
|
||||
@Test
|
||||
public void test1(){
|
||||
WifeMapper mapper = SqlSessionUtil.getSqlSession().getMapper(WifeMapper.class);
|
||||
mapper.getWifeAll().forEach(System.out::println);
|
||||
}
|
||||
}
|
Loading…
x
Reference in New Issue
Block a user