Skip to content

Commit

Permalink
使用MyBatis XML方式建立持久層及其測試方法
Browse files Browse the repository at this point in the history
  • Loading branch information
kaijun committed Nov 17, 2021
1 parent 003a112 commit 4077dfa
Show file tree
Hide file tree
Showing 10 changed files with 99 additions and 79 deletions.
2 changes: 2 additions & 0 deletions src/main/java/com/example/demo/SpringBootDemoApplication.java
@@ -1,9 +1,11 @@
package com.example.demo;

import org.mybatis.spring.annotation.MapperScan;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;

@SpringBootApplication
@MapperScan("com.example.demo.mapper")
public class SpringBootDemoApplication {

public static void main(String[] args) {
Expand Down
8 changes: 0 additions & 8 deletions src/main/java/com/example/demo/mapper/CountyMapper.java
Expand Up @@ -2,18 +2,10 @@

import java.util.List;

import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Select;

import com.example.demo.entity.County;

@Mapper
public interface CountyMapper {

@Select(" SELECT "
+ " ID, NAME "
+ " FROM "
+ " test_project.county ")
public List<County> findAllCounty();

}
10 changes: 0 additions & 10 deletions src/main/java/com/example/demo/mapper/DistrictMapper.java
Expand Up @@ -2,20 +2,10 @@

import java.util.List;

import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Select;

import com.example.demo.entity.District;

@Mapper
public interface DistrictMapper {

@Select(" SELECT "
+ " ID, NAME "
+ " FROM "
+ " test_project.district "
+ " WHERE "
+ " C_ID = #{c_id} ")
public List<District> findDistrictByC_id(String c_id);

}
28 changes: 0 additions & 28 deletions src/main/java/com/example/demo/mapper/MemberAccountMapper.java
@@ -1,39 +1,11 @@
package com.example.demo.mapper;

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

import com.example.demo.entity.MemberAccount;

@Mapper
public interface MemberAccountMapper {

@Insert(" INSERT INTO test_project.member_account ( "
+ " USERNAME, PASSWORD, SALT, "
+ " CREATE_BY, CREATE_TIME, UPDATE_BY, UPDATE_TIME "
+ " ) "
+ " VALUE ( "
+ " #{username}, #{password}, #{salt}, "
+ " #{create_by}, NOW(), #{update_by}, NOW() "
+ " ) ")
public Integer insert(MemberAccount memberAccount);

@Select(" SELECT "
+ " ID, USERNAME, PASSWORD, SALT "
+ " FROM "
+ " test_project.member_account "
+ " WHERE "
+ " USERNAME = #{username} ")
public MemberAccount findMemberAccountByUsername(String username);

@Update(" UPDATE "
+ " test_project.member_account "
+ " SET "
+ " PASSWORD = #{password}, UPDATE_BY = #{update_by}, UPDATE_TIME = NOW() "
+ " WHERE "
+ " ID = #{id} ")
public Integer update(MemberAccount memberAccount);

}
33 changes: 0 additions & 33 deletions src/main/java/com/example/demo/mapper/MemberMapper.java
@@ -1,44 +1,11 @@
package com.example.demo.mapper;

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

import com.example.demo.entity.Member;

@Mapper
public interface MemberMapper {

@Insert(" INSERT INTO test_project.member ( "
+ " MA_ID, NAME, ID_NUMBER, BIRTHDAY, "
+ " PHONE, C_ID, D_ID, ADDRESS, "
+ " CREATE_BY, CREATE_TIME, UPDATE_BY, UPDATE_TIME "
+ " ) "
+ " VALUE ( "
+ " #{ma_id}, #{name}, #{id_number}, #{birthday}, "
+ " #{phone}, #{c_id}, #{d_id}, #{address}, "
+ " #{create_by}, NOW(), #{update_by}, NOW() "
+ " ) ")
public Integer insert(Member member);

@Select(" SELECT "
+ " MA_ID, NAME, ID_NUMBER, BIRTHDAY, "
+ " PHONE, C_ID, D_ID, ADDRESS "
+ " FROM "
+ " test_project.member "
+ " WHERE "
+ " MA_ID = #{ma_id} ")
public Member findMemberByMa_id(String ma_id);

@Update(" UPDATE "
+ " test_project.member "
+ " SET "
+ " ID_NUMBER = #{id_number}, BIRTHDAY = #{birthday}, PHONE = #{phone}, "
+ " C_ID = #{c_id}, D_ID = #{d_id}, ADDRESS = #{address}, "
+ " UPDATE_BY = #{update_by}, UPDATE_TIME = NOW() "
+ " WHERE "
+ " MA_ID = #{ma_id} ")
public Integer update(Member member);

}
2 changes: 2 additions & 0 deletions src/main/resources/application.properties
Expand Up @@ -2,3 +2,5 @@ spring.datasource.url=jdbc:mysql://localhost:3306/test_project?useUnicode=true&c
spring.datasource.username=root
spring.datasource.password=123456
spring.datasource.driver-class-name=com.mysql.cj.jdbc.Driver

mybatis.mapper-locations=classpath:mappers/*.xml
11 changes: 11 additions & 0 deletions src/main/resources/mappers/CountyMapper.xml
@@ -0,0 +1,11 @@
<?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">
<!-- namespace 對應持久層的介面 -->
<mapper namespace="com.example.demo.mapper.CountyMapper" >
<select id="findAllCounty" resultType="com.example.demo.entity.County">
SELECT
ID, NAME
FROM
test_project.county
</select>
</mapper>
13 changes: 13 additions & 0 deletions src/main/resources/mappers/DistrictMapper.xml
@@ -0,0 +1,13 @@
<?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">
<!-- namespace 對應持久層的介面 -->
<mapper namespace="com.example.demo.mapper.DistrictMapper" >
<select id="findDistrictByC_id" resultType="com.example.demo.entity.District">
SELECT
ID, C_ID, NAME
FROM
test_project.district
WHERE
C_ID = #{c_id}
</select>
</mapper>
33 changes: 33 additions & 0 deletions src/main/resources/mappers/MemberAccountMapper.xml
@@ -0,0 +1,33 @@
<?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">
<!-- namespace 對應持久層的介面 -->
<mapper namespace="com.example.demo.mapper.MemberAccountMapper" > <!-- id 對應持久層介面的方法,parameterType 為引數資料型別,resultType 為返回資料型別 -->
<insert id="insert" parameterType="com.example.demo.entity.MemberAccount">
INSERT INTO test_project.member_account (
USERNAME, PASSWORD, SALT,
CREATE_BY, CREATE_TIME, UPDATE_BY, UPDATE_TIME
)
VALUES (
#{username}, #{password}, #{salt},
#{create_by}, NOW(), #{update_by}, NOW()
)
</insert>

<select id="findMemberAccountByUsername" parameterType="string" resultType="com.example.demo.entity.MemberAccount">
SELECT
ID, USERNAME, PASSWORD, SALT
FROM
test_project.member_account
WHERE
USERNAME = #{username}
</select>

<update id="update" parameterType="com.example.demo.entity.MemberAccount">
UPDATE
test_project.member_account
SET
PASSWORD = #{password}, UPDATE_BY = #{update_by}, UPDATE_TIME = NOW()
WHERE
ID = #{id}
</update>
</mapper>
38 changes: 38 additions & 0 deletions src/main/resources/mappers/MemberMapper.xml
@@ -0,0 +1,38 @@
<?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">
<!-- namespace 對應持久層的介面 -->
<mapper namespace="com.example.demo.mapper.MemberMapper" > <!-- id 對應持久層介面的方法,parameterType 為引數資料型別,resultType 為返回資料型別 -->
<insert id="insert" parameterType="com.example.demo.entity.Member">
INSERT INTO test_project.member (
MA_ID, NAME, ID_NUMBER, BIRTHDAY,
PHONE, C_ID, D_ID, ADDRESS,
CREATE_BY, CREATE_TIME, UPDATE_BY, UPDATE_TIME
)
VALUES (
#{ma_id}, #{name}, #{id_number}, #{birthday},
#{phone}, #{c_id}, #{d_id}, #{address},
#{create_by}, NOW(), #{update_by}, NOW()
)
</insert>

<select id="findMemberByMa_id" parameterType="string" resultType="com.example.demo.entity.Member">
SELECT
MA_ID, NAME, ID_NUMBER, BIRTHDAY,
PHONE, C_ID, D_ID, ADDRESS
FROM
test_project.member
WHERE
MA_ID = #{ma_id}
</select>

<update id="update" parameterType="com.example.demo.entity.Member">
UPDATE
test_project.member
SET
ID_NUMBER = #{id_number}, BIRTHDAY = #{birthday}, PHONE = #{phone},
C_ID = #{c_id}, D_ID = #{d_id}, ADDRESS = #{address},
UPDATE_BY = #{update_by}, UPDATE_TIME = NOW()
WHERE
MA_ID = #{ma_id}
</update>
</mapper>

0 comments on commit 4077dfa

Please sign in to comment.