-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
rpish
committed
Sep 14, 2021
1 parent
8e2b16a
commit d314427
Showing
19 changed files
with
394 additions
and
0 deletions.
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
<?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"> | ||
<parent> | ||
<artifactId>SSM-bootstrap</artifactId> | ||
<groupId>org.example</groupId> | ||
<version>1.0-SNAPSHOT</version> | ||
</parent> | ||
<modelVersion>4.0.0</modelVersion> | ||
|
||
<artifactId>MyBatis-02</artifactId> | ||
<dependencies> | ||
<dependency> | ||
<groupId>org.mybatis</groupId> | ||
<artifactId>mybatis</artifactId> | ||
<version>3.5.7</version> | ||
</dependency> | ||
</dependencies> | ||
|
||
<properties> | ||
<maven.compiler.source>8</maven.compiler.source> | ||
<maven.compiler.target>8</maven.compiler.target> | ||
</properties> | ||
|
||
</project> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,50 @@ | ||
package tech.rpish.po; | ||
|
||
public class User { | ||
private Integer id; | ||
private String username; | ||
private String job; | ||
private String phone; | ||
|
||
@Override | ||
public String toString() { | ||
return "User{" + | ||
"id=" + id + | ||
", username='" + username + '\'' + | ||
", job='" + job + '\'' + | ||
", phone='" + phone + '\'' + | ||
'}'; | ||
} | ||
|
||
public Integer getId() { | ||
return id; | ||
} | ||
|
||
public void setId(Integer id) { | ||
this.id = id; | ||
} | ||
|
||
public String getUsername() { | ||
return username; | ||
} | ||
|
||
public void setUsername(String username) { | ||
this.username = username; | ||
} | ||
|
||
public String getJob() { | ||
return job; | ||
} | ||
|
||
public void setJob(String job) { | ||
this.job = job; | ||
} | ||
|
||
public String getPhone() { | ||
return phone; | ||
} | ||
|
||
public void setPhone(String phone) { | ||
this.phone = phone; | ||
} | ||
} |
28 changes: 28 additions & 0 deletions
28
MyBatis-02/src/main/java/tech/rpish/util/MyBatisUtils.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
package tech.rpish.util; | ||
|
||
import org.apache.ibatis.session.SqlSession; | ||
import org.apache.ibatis.session.SqlSessionFactory; | ||
|
||
import org.apache.ibatis.io.Resources; | ||
import org.apache.ibatis.session.SqlSessionFactoryBuilder; | ||
|
||
import java.io.IOException; | ||
import java.io.InputStream; | ||
|
||
public class MyBatisUtils { | ||
private static SqlSessionFactory sqlSessionFactory = null; | ||
static { | ||
try { | ||
String resource = "mybatis-config.xml"; | ||
InputStream inputStream = Resources.getResourceAsStream(resource); | ||
sqlSessionFactory = new SqlSessionFactoryBuilder().build(inputStream); | ||
|
||
} catch (IOException e) { | ||
e.printStackTrace(); | ||
} | ||
} | ||
|
||
public static SqlSession getSession() { | ||
return sqlSessionFactory.openSession(); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
jdbc.driver=com.mysql.jdbc.Driver | ||
jdbc.url=jdbc:mysql://localhost:3306/ssm_bootstrap | ||
jdbc.username=root | ||
jdbc.password=pass |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
# Global logging configuration | ||
log4j.rootLogger=ERROR, stdout | ||
# MyBatis logging configuration... | ||
log4j.logger.com.ssm=DEBUG | ||
Console output... | ||
log4j.appender.stdout=org.apache.log4j.ConsoleAppender | ||
log4j.appender.stdout.layout=org.apache.log4j.PatternLayout | ||
log4j.appender.stdout.layout.ConversionPattern=%5p [%t] - %m%n |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,62 @@ | ||
<?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="tech.rpish.mapper.UserMapper"> | ||
<select id="findUserByNameAndJob" parameterType="tech.rpish.po.User" resultType="tech.rpish.po.User"> | ||
select * from user | ||
<where> | ||
<if test="username !=null and username !=''"> | ||
and username like concat('%',#{username},'%') | ||
</if> | ||
<if test="job != null and job !=''"> | ||
and job = #{job} | ||
</if> | ||
</where> | ||
</select> | ||
|
||
<select id="findUserByNameOrJob" parameterType="tech.rpish.po.User" resultType="tech.rpish.po.User"> | ||
select * from user | ||
<where> | ||
<choose> | ||
<when test="username != null and username !=''"> | ||
and username like concat('%',#{username},'%') | ||
</when> | ||
<when test="job != null and job != ''"> | ||
and job=#{job} | ||
</when> | ||
<otherwise> | ||
and phone is not null | ||
</otherwise> | ||
</choose> | ||
</where> | ||
</select> | ||
|
||
<update id="updateUser" parameterType="tech.rpish.po.User"> | ||
update user | ||
<set> | ||
<if test="username != null and username != ''"> | ||
username=#{username}, | ||
</if> | ||
<if test="job != null and job!=''"> | ||
job=#{job}, | ||
</if> | ||
<if test="phone!=null and phone!=''"> | ||
phone=#{phone} | ||
</if> | ||
</set> | ||
where id=#{id} | ||
</update> | ||
|
||
<select id="findUserByIds" parameterType="List" resultType="tech.rpish.po.User"> | ||
select * from user where id in | ||
<foreach item="id" index="index" collection="list" open="(" separator="," close=")"> | ||
#{id} | ||
</foreach> | ||
</select> | ||
|
||
<select id="findUserByName" parameterType="tech.rpish.po.User" resultType="tech.rpish.po.User"> | ||
<bind name="p_username" value="'%'+username+'%'"/> | ||
select * from user | ||
where username like #{p_username} | ||
</select> | ||
</mapper> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
<?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 resource="db.properties"/> | ||
<environments default="mysql"> | ||
<environment id="mysql"> | ||
<transactionManager type="JDBC"></transactionManager> | ||
<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> | ||
<mapper resource="mapper/UserMapper.xml"></mapper> | ||
</mappers> | ||
</configuration> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,80 @@ | ||
import org.apache.ibatis.session.SqlSession; | ||
import org.junit.Test; | ||
import tech.rpish.po.User; | ||
import tech.rpish.util.MyBatisUtils; | ||
|
||
import java.util.ArrayList; | ||
import java.util.List; | ||
|
||
public class MyBatisTest { | ||
@Test | ||
public void findUserByNameAndJobTest() throws Exception { | ||
SqlSession sqlSession = MyBatisUtils.getSession(); | ||
User user = new User(); | ||
user.setUsername("f"); | ||
user.setJob("fo"); | ||
List<User> users = sqlSession.selectList("tech.rpish.mapper.UserMapper.findUserByNameAndJob", user); | ||
for (User u : | ||
users) { | ||
System.out.println(u); | ||
} | ||
sqlSession.close(); | ||
} | ||
|
||
@Test | ||
public void findUserByNameOrJobTest() throws Exception { | ||
SqlSession session = MyBatisUtils.getSession(); | ||
User user = new User(); | ||
user.setUsername("fo"); | ||
user.setJob("f"); | ||
List<User> users = session.selectList("tech.rpish.mapper.UserMapper.findUserByNameOrJob", user); | ||
for (User u : | ||
users) { | ||
System.out.println(u); | ||
} | ||
session.close(); | ||
} | ||
|
||
@Test | ||
public void updateUserTest() throws Exception { | ||
SqlSession session = MyBatisUtils.getSession(); | ||
User user = new User(); | ||
user.setId(1); | ||
user.setPhone("220"); | ||
int rows = session.update("tech.rpish.mapper.UserMapper.updateUser", user); | ||
if (rows > 0) { | ||
System.out.println("成功修改" + rows + "条数据"); | ||
} else { | ||
System.out.println("修改失败"); | ||
} | ||
session.commit(); | ||
session.close(); | ||
} | ||
|
||
@Test | ||
public void findUserByIdsTest() throws Exception { | ||
SqlSession session = MyBatisUtils.getSession(); | ||
List<Integer> ids = new ArrayList<Integer>(); | ||
ids.add(1); | ||
ids.add(2); | ||
List<User> users = session.selectList("tech.rpish.mapper.UserMapper.findUserByIds", ids); | ||
for (User u : | ||
users) { | ||
System.out.println(u); | ||
} | ||
session.close(); | ||
} | ||
|
||
@Test | ||
public void findUserByName() { | ||
SqlSession session = MyBatisUtils.getSession(); | ||
User user = new User(); | ||
user.setUsername("f"); | ||
List<User> users = session.selectList("tech.rpish.mapper.UserMapper.findUserByName", user); | ||
for (User u : users | ||
) { | ||
System.out.println(u); | ||
} | ||
session.close(); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
jdbc.driver=com.mysql.jdbc.Driver | ||
jdbc.url=jdbc:mysql://localhost:3306/ssm_bootstrap | ||
jdbc.username=root | ||
jdbc.password=pass |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
# Global logging configuration | ||
log4j.rootLogger=ERROR, stdout | ||
# MyBatis logging configuration... | ||
log4j.logger.com.ssm=DEBUG | ||
Console output... | ||
log4j.appender.stdout=org.apache.log4j.ConsoleAppender | ||
log4j.appender.stdout.layout=org.apache.log4j.PatternLayout | ||
log4j.appender.stdout.layout.ConversionPattern=%5p [%t] - %m%n |
Oops, something went wrong.