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
kaijun
committed
Nov 17, 2021
1 parent
b6e2107
commit a4c8538
Showing
13 changed files
with
424 additions
and
0 deletions.
There are no files selected for viewing
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
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,11 @@ | ||
package com.example.demo.dao; | ||
|
||
import java.util.List; | ||
|
||
import com.example.demo.entity.County; | ||
|
||
public interface CountyDao { | ||
|
||
public List<County> findAllCounty(); | ||
|
||
} |
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,11 @@ | ||
package com.example.demo.dao; | ||
|
||
import java.util.List; | ||
|
||
import com.example.demo.entity.District; | ||
|
||
public interface DistrictDao { | ||
|
||
public List<District> findDistrictByC_id(String c_id); | ||
|
||
} |
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,11 @@ | ||
package com.example.demo.dao; | ||
|
||
import com.example.demo.entity.MemberAccount; | ||
|
||
public interface MemberAccountDao { | ||
|
||
public Integer insert(MemberAccount memberAccount); | ||
public MemberAccount findMemberAccountByUsername(String username); | ||
public Integer update(MemberAccount memberAccount); | ||
|
||
} |
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,11 @@ | ||
package com.example.demo.dao; | ||
|
||
import com.example.demo.entity.Member; | ||
|
||
public interface MemberDao { | ||
|
||
public Integer insert(Member member); | ||
public Member findMemberByMa_id(String ma_id); | ||
public Integer update(Member member); | ||
|
||
} |
36 changes: 36 additions & 0 deletions
36
src/main/java/com/example/demo/dao/impl/CountyDaoImpl.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,36 @@ | ||
package com.example.demo.dao.impl; | ||
|
||
import java.util.List; | ||
|
||
import org.springframework.beans.factory.annotation.Autowired; | ||
import org.springframework.jdbc.core.BeanPropertyRowMapper; | ||
import org.springframework.jdbc.core.JdbcTemplate; | ||
import org.springframework.stereotype.Repository; | ||
|
||
import com.example.demo.dao.CountyDao; | ||
import com.example.demo.entity.County; | ||
|
||
@Repository | ||
public class CountyDaoImpl implements CountyDao { | ||
|
||
@Autowired | ||
private JdbcTemplate jdbcTemplate; | ||
|
||
@Override | ||
public List<County> findAllCounty() { | ||
// TODO Auto-generated method stub | ||
String sql = " SELECT " | ||
+ " ID, NAME " | ||
+ " FROM " | ||
+ " test_project.county " | ||
+ " ORDER BY " | ||
+ " ID "; | ||
|
||
List<County> result = jdbcTemplate.query(sql, new BeanPropertyRowMapper<County>(County.class)); | ||
if(result != null && result.size() > 0) { | ||
return result; | ||
} | ||
return null; | ||
} | ||
|
||
} |
36 changes: 36 additions & 0 deletions
36
src/main/java/com/example/demo/dao/impl/DistrictDaoImpl.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,36 @@ | ||
package com.example.demo.dao.impl; | ||
|
||
import java.util.List; | ||
|
||
import org.springframework.beans.factory.annotation.Autowired; | ||
import org.springframework.jdbc.core.BeanPropertyRowMapper; | ||
import org.springframework.jdbc.core.JdbcTemplate; | ||
import org.springframework.stereotype.Repository; | ||
|
||
import com.example.demo.dao.DistrictDao; | ||
import com.example.demo.entity.District; | ||
|
||
@Repository | ||
public class DistrictDaoImpl implements DistrictDao { | ||
|
||
@Autowired | ||
private JdbcTemplate jdbcTemplate; | ||
|
||
@Override | ||
public List<District> findDistrictByC_id(String c_id) { | ||
// TODO Auto-generated method stub | ||
String sql = " SELECT " | ||
+ " ID, C_ID, NAME " | ||
+ " FROM " | ||
+ " test_project.district " | ||
+ " WHERE " | ||
+ " C_ID = ? "; | ||
|
||
List<District> result = jdbcTemplate.query(sql, new BeanPropertyRowMapper<District>(District.class), new Object[] { c_id }); | ||
if(result != null && result.size() > 0) { | ||
return result; | ||
} | ||
return null; | ||
} | ||
|
||
} |
77 changes: 77 additions & 0 deletions
77
src/main/java/com/example/demo/dao/impl/MemberAccountDaoImpl.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,77 @@ | ||
package com.example.demo.dao.impl; | ||
|
||
import java.util.List; | ||
|
||
import org.springframework.beans.factory.annotation.Autowired; | ||
import org.springframework.jdbc.core.BeanPropertyRowMapper; | ||
import org.springframework.jdbc.core.JdbcTemplate; | ||
import org.springframework.jdbc.core.namedparam.BeanPropertySqlParameterSource; | ||
import org.springframework.jdbc.core.namedparam.NamedParameterJdbcTemplate; | ||
import org.springframework.jdbc.core.namedparam.SqlParameterSource; | ||
import org.springframework.jdbc.support.GeneratedKeyHolder; | ||
import org.springframework.jdbc.support.KeyHolder; | ||
import org.springframework.stereotype.Repository; | ||
|
||
import com.example.demo.dao.MemberAccountDao; | ||
import com.example.demo.entity.MemberAccount; | ||
|
||
@Repository | ||
public class MemberAccountDaoImpl implements MemberAccountDao { | ||
|
||
@Autowired | ||
private JdbcTemplate jdbcTemplate; | ||
|
||
@Autowired | ||
private NamedParameterJdbcTemplate jdbcNameTemplate; | ||
|
||
@Override | ||
public Integer insert(MemberAccount memberAccount) { | ||
// TODO Auto-generated method stub | ||
String sql = " 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() " | ||
+ " ) "; | ||
|
||
SqlParameterSource paramSource = new BeanPropertySqlParameterSource(memberAccount); | ||
KeyHolder keyHolder = new GeneratedKeyHolder(); | ||
|
||
jdbcNameTemplate.update(sql, paramSource, keyHolder); | ||
return keyHolder.getKey().intValue(); | ||
} | ||
|
||
@Override | ||
public MemberAccount findMemberAccountByUsername(String username) { | ||
// TODO Auto-generated method stub | ||
String sql = " SELECT " | ||
+ " ID, USERNAME, PASSWORD, SALT " | ||
+ " FROM " | ||
+ " test_project.member_account " | ||
+ " WHERE " | ||
+ " USERNAME = ? "; | ||
|
||
List<MemberAccount> result = jdbcTemplate.query(sql, new BeanPropertyRowMapper<MemberAccount>(MemberAccount.class), new Object[] { username }); | ||
if(result != null && result.size() > 0) { | ||
return result.get(0); | ||
} | ||
return null; | ||
} | ||
|
||
@Override | ||
public Integer update(MemberAccount memberAccount) { | ||
// TODO Auto-generated method stub | ||
String sql = " UPDATE " | ||
+ " test_project.member_account " | ||
+ " SET " | ||
+ " PASSWORD = :password, UPDATE_BY = :update_by, UPDATE_TIME = NOW() " | ||
+ " WHERE " | ||
+ " ID = :id "; | ||
|
||
SqlParameterSource paramSource = new BeanPropertySqlParameterSource(memberAccount); | ||
return jdbcNameTemplate.update(sql, paramSource); | ||
} | ||
|
||
} |
73 changes: 73 additions & 0 deletions
73
src/main/java/com/example/demo/dao/impl/MemberDaoImpl.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,73 @@ | ||
package com.example.demo.dao.impl; | ||
|
||
import java.util.List; | ||
|
||
import org.springframework.beans.factory.annotation.Autowired; | ||
import org.springframework.jdbc.core.BeanPropertyRowMapper; | ||
import org.springframework.jdbc.core.JdbcTemplate; | ||
import org.springframework.jdbc.core.namedparam.BeanPropertySqlParameterSource; | ||
import org.springframework.jdbc.core.namedparam.NamedParameterJdbcTemplate; | ||
import org.springframework.jdbc.core.namedparam.SqlParameterSource; | ||
import org.springframework.stereotype.Repository; | ||
|
||
import com.example.demo.dao.MemberDao; | ||
import com.example.demo.entity.Member; | ||
|
||
@Repository | ||
public class MemberDaoImpl implements MemberDao { | ||
|
||
@Autowired | ||
private JdbcTemplate jdbcTemplate; | ||
|
||
@Autowired | ||
private NamedParameterJdbcTemplate jdbcNameTemplate; | ||
|
||
@Override | ||
public Integer insert(Member member) { | ||
// TODO Auto-generated method stub | ||
String sql = " 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() " | ||
+ " ) "; | ||
|
||
SqlParameterSource paramSource = new BeanPropertySqlParameterSource(member); | ||
return jdbcNameTemplate.update(sql, paramSource); | ||
} | ||
|
||
@Override | ||
public Member findMemberByMa_id(String ma_id) { | ||
// TODO Auto-generated method stub | ||
String sql = " SELECT " | ||
+ " MA_ID, NAME, ID_NUMBER, BIRTHDAY, PHONE, C_ID, D_ID, ADDRESS " | ||
+ " FROM " | ||
+ " test_project.member " | ||
+ " WHERE " | ||
+ " MA_ID = ? "; | ||
|
||
List<Member> result = jdbcTemplate.query(sql, new BeanPropertyRowMapper<Member>(Member.class), new Object[] { ma_id }); | ||
if(result != null && result.size() > 0) { | ||
return result.get(0); | ||
} | ||
return null; | ||
} | ||
|
||
@Override | ||
public Integer update(Member member) { | ||
// TODO Auto-generated method stub | ||
String sql = " 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 "; | ||
|
||
SqlParameterSource paramSource = new BeanPropertySqlParameterSource(member); | ||
return jdbcNameTemplate.update(sql, paramSource); | ||
} | ||
|
||
} |
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,23 @@ | ||
package com.example.demo.dao; | ||
|
||
import java.util.List; | ||
|
||
import org.junit.jupiter.api.Test; | ||
import org.springframework.beans.factory.annotation.Autowired; | ||
import org.springframework.boot.test.context.SpringBootTest; | ||
|
||
import com.example.demo.entity.County; | ||
|
||
@SpringBootTest | ||
public class CountyDaoTest { | ||
|
||
@Autowired | ||
private CountyDao countyDao; | ||
|
||
@Test | ||
public void findAllCounty() { | ||
List<County> list = countyDao.findAllCounty(); | ||
if(list != null) list.forEach(county -> System.out.println(county.toString())); | ||
} | ||
|
||
} |
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,24 @@ | ||
package com.example.demo.dao; | ||
|
||
import java.util.List; | ||
|
||
import org.junit.jupiter.api.Test; | ||
import org.springframework.beans.factory.annotation.Autowired; | ||
import org.springframework.boot.test.context.SpringBootTest; | ||
|
||
import com.example.demo.entity.District; | ||
|
||
@SpringBootTest | ||
public class DistrictDaoTest { | ||
|
||
@Autowired | ||
private DistrictDao districtDao; | ||
|
||
@Test | ||
public void findDistrictByC_id() { | ||
String c_id = "1"; | ||
List<District> list = districtDao.findDistrictByC_id(c_id); | ||
if(list != null) list.forEach(district -> System.out.println(district.toString())); | ||
} | ||
|
||
} |
50 changes: 50 additions & 0 deletions
50
src/test/java/com/example/demo/dao/MemberAccountDaoTest.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,50 @@ | ||
package com.example.demo.dao; | ||
|
||
import java.util.UUID; | ||
|
||
import org.junit.jupiter.api.Test; | ||
import org.springframework.beans.factory.annotation.Autowired; | ||
import org.springframework.boot.test.context.SpringBootTest; | ||
|
||
import com.example.demo.entity.MemberAccount; | ||
|
||
@SpringBootTest | ||
public class MemberAccountDaoTest { | ||
|
||
@Autowired | ||
private MemberAccountDao memberAccountDao; | ||
|
||
@Test | ||
public void insert() { | ||
MemberAccount memberAccount = new MemberAccount(); | ||
memberAccount.setUsername("username@email.com"); | ||
memberAccount.setPassword("password"); | ||
|
||
String salt = UUID.randomUUID().toString().toUpperCase().replaceAll("-", ""); | ||
memberAccount.setSalt(salt); | ||
memberAccount.setCreate_by(memberAccount.getUsername()); | ||
memberAccount.setUpdate_by(memberAccount.getUsername()); | ||
|
||
Integer id = memberAccountDao.insert(memberAccount); | ||
System.out.println(id); | ||
} | ||
|
||
@Test | ||
public void findMemberAccountByUsername() { | ||
String username = "username@email.com"; | ||
MemberAccount memberAccount = memberAccountDao.findMemberAccountByUsername(username); | ||
if(memberAccount != null) System.out.println(memberAccount.toString()); | ||
} | ||
|
||
@Test | ||
public void update() { | ||
MemberAccount memberAccount = new MemberAccount(); | ||
memberAccount.setId("1"); | ||
memberAccount.setPassword("123456"); | ||
memberAccount.setUpdate_by("username@email.com"); | ||
|
||
Integer result = memberAccountDao.update(memberAccount); | ||
System.out.println(result); | ||
} | ||
|
||
} |
Oops, something went wrong.