In him was life, and that life was the light of all mankind.
[John 1:4]
2. Database
2. 3 . MyBatis
MyBatis is one of the Java persistence frameworks that provides developers with a set of SQL mapping and configuration files to simplify database access. It enables mapping Java objects to SQL statements and vice versa.
1. Object-Relational Mapping (ORM)**: MyBatis bridges the gap between object-oriented programming and relational databases by mapping Java objects to SQL statements and database tables. This eliminates the need for manual SQL queries and result set processing, simplifying interaction between Java applications and databases.
2. XML Configuration Files: MyBatis uses XML configuration files to define SQL mappings and database connection settings. These files specify SQL queries and mappings between Java objects and database columns. Below are the key components of each XML configuration file:
3. Automatic Mapping: MyBatis provides the capability to automatically map result sets to Java objects. This allows developers to easily convert database results to Java objects without writing additional code.
4. Various Features: MyBatis offers various features to help developers manage database access more easily. These include dynamic SQL, transaction management, caching, and more.
MyBatis is lightweight, flexible, and supports productive ways of managing database access, making it very popular among Java developers..
Here's a detailed list of the topics
- 2.3.1 Basic Springboot MyBaits Project:
- 2.3.2 MyBatis Configuration:
2.3.1 Basic MyBatis Project:
[ File ] - [ New ] - [ Spring Start Project ]
2.3.2 MyBatis Configuration:
application.properties
spring.application.name=MyBatisExam
# database setting
spring.datasource.driver-class-name=com.mysql.cj.jdbc.Driver
spring.datasource.url=jdbc:mysql://localhost:3306/mybatisexam
spring.datasource.username=root
spring.datasource.password=1111
# Mybatis mapper setting
mybatis.mapper-locations=classpath:mybatis/mapper/*.xml
Mapper
In MyBatis, a Mapper is an interface that defines the mapping between SQL queries and Java methods. Through this, developers can define and execute SQL queries using Java interfaces instead of writing SQL queries using XML files or annotations. This enhances code readability and maintainability, reducing coupling between SQL and Java code.
user.xml
Basic Grammer
parameterType - input data type
resultType - output data type
<![CDATA[...]]> - SQLs
<?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="user">
<insert id="create"
parameterType="com.example.mybatisexam.User">
<![CDATA[
INSERT INTO user
(uid, upw)
VALUES
(#{uid}, #{upw})
]]>
</insert>
<select id="readlist"
resultType="com.example.mybatisexam.User">
<![CDATA[
SELECT *
FROM user
]]>
</select>
<select id="readdetail" parameterType="String"
resultType="com.example.mybatisexam.User">
<![CDATA[
SELECT *
FROM user
where uno= #{uno}
]]>
</select>
<update id="update"
parameterType="com.example.mybatisexam.User">
<![CDATA[
UPDATE user
SET uid = #{uid},
upw = #{upw}
WHERE uno = #{uno}
]]>
</update>
<delete id="delete" parameterType="String">
<![CDATA[
DELETE FROM user
WHERE uno = #{uno}
]]>
</delete>
</mapper>
2.3.3 Create, Read (list, detail):
Repository
@Autowired
private SqlSessionTemplate sqlSessionTemplate;
@Override
public void create(User user) {
sqlSessionTemplate.insert("user.create", user);
}
@Override
public List<User> readlist() {
return sqlSessionTemplate.selectList("user.readlist");
}
@Override
public User readdetail(String uno) {
return sqlSessionTemplate.selectOne("user.readdetail", uno);
}
}
Service
@Autowired
private UserRepository userRepository;
@Override
public void create(User user) {
userRepository.create(user);
}
@Override
public List<User> readlist() {
return userRepository.readlist();
}
@Override
public User readdetail(String uno) {
return userRepository.readdetail(uno);
}
Controller
@Autowired
private UserService userService;
@GetMapping("/create")
public String index() {
return "create";
}
@PostMapping("/create")
public String index(User user) {
userService.create(user);
return "create";
}
@GetMapping("/")
public String readlist(Model model) {
model.addAttribute("users", userService.readlist());
return "readlist";
}
@GetMapping("/readdetail/{uno}")
public String readlist(Model model, @PathVariable ("uno") String uno) {
model.addAttribute("user", userService.readdetail(uno));
return "readdetail";
}
create.html
<!DOCTYPE html>
<html>
<head>
<meta charset="EUC-KR">
<title>Insert title here</title>
</head>
<body>
<h2> Create </h2>
<form action="/create" method="post">
<p>id : <input type="text" name="uid">
<p>pw : <input type="text" name="upw">
<p><input type="submit" value="join">
</form>
</body>
</html>
readlist.html
<!DOCTYPE html>
<html>
<head>
<meta charset="EUC-KR">
<title>Insert title here</title>
</head>
<body>
<h2> Read List </h2>
<a href="/create">CREATE</a><br><br>
<th:block th:each="user: ${users}">
[[${user.uno}]] /
<a th:href="@{|/readdetail/${user.uno}|}" th:text="${user.uid}"></a>
/ [[${user.upw}]] <br>
</th:block>
</body>
</html>
readdetail.html
<!DOCTYPE html>
<html>
<head>
<meta charset="EUC-KR">
<title>Insert title here</title>
</head>
<body>
<h2> Read Detail </h2>
[[${user.uno}]] <br>
[[${user.uid}]] <br>
[[${user.upw}]] <br>
</body>
</html>
2.3.4 Update, Delete:
repository
@Override
public void update(User user) {
sqlSessionTemplate.update("user.update", user);
}
@Override
public void delete(String username) {
sqlSessionTemplate.delete("user.delete", username);
}
Service
@Override
public void update(User user) {
userRepository.update(user);
}
@Override
public void delete(String username) {
userRepository.delete(username);
}
Controller
@GetMapping("/update/{uno}")
public String update(Model model, @PathVariable ("uno") String uno) {
model.addAttribute("user", userService.readdetail(uno));
return "update";
}
@PostMapping("/update")
public String update(User user) {
userService.update(user);
return "redirect:/";
}
@GetMapping("/delete/{uno}")
public String delete(@PathVariable ("uno") String uno) {
userService.delete(uno);
return "redirect:/";
}
update.html
<!DOCTYPE html>
<html>
<head>
<meta charset="EUC-KR">
<title>Insert title here</title>
</head>
<body>
<h2> Update </h2>
<form action="/update" method="post">
<input type="hidden" name="uno" th:value="${user.uno}">
<p>id : <input type="text" name="uid" th:value="${user.uid}">
<p>pw : <input type="text" name="upw" th:value="${user.upw}">
<p><input type="submit" value="update">
</form>
</body>
</html>
readdetail.html
<!DOCTYPE html>
<html>
<head>
<meta charset="EUC-KR">
<title>Insert title here</title>
</head>
<body>
<h2> Read Detail </h2>
[[${user.uno}]] <br>
[[${user.uid}]] <br>
[[${user.upw}]] <br>
<a th:href="@{|/update/${user.uno}|}">update</a><br>
<a th:href="@{|/delete/${user.uno}|}">delete</a><br>
</body>
</html>