描述使用Mybatis來進行資料庫操作的過程
阿新 • • 發佈:2019-01-06
什麼是Mybatis?維基百科是這樣定義的:Mybatis是一個Java持久化框架,它通過XML描述符或註解把物件與儲存過程或SQL語句關聯起來。通俗點說就是Mybatis是對JPA(Java持久化API,一個Java應用程式介面規範)的實現,是為了簡化資料庫操作過程而衍生出來的產品。
----------------------------------------------------------------------------------------------------------------------------------------------------------------------------
使用步驟:(以IDEA中為例)
1.在application.yml中進行配置資料來源等資訊
spring: datasource: driver-class-name: com.mysql.cj.jdbc.Driver url: jdbc:mysql://127.0.0.1:3306/bookshop?useUnicode=true&characterEncoding=UTF-8&serverTimezone=UTC username: root password: root servlet: multipart: enabled: true max-request-size: 2MB max-file-size: 2MB mybatis: config-location: classpath:/mybatis/mybatis_config.xml mapper-locations: classpath:/mybatis/mapper/*.xml server: port: 8080
2.建立好對應的模型類(User):
package com.bookShop.test.model; public class User { private int user_id; private String user_name; private String user_password; private int user_pin; private String user_sex; private double user_wallet; private String user_email; private String user_phonenum; public User() { } public User(String user_name, String user_password, String user_sex, String user_phonenum) { this.user_name = user_name; this.user_password = user_password; this.user_sex = user_sex; this.user_phonenum = user_phonenum; } public User(String user_password, String user_phonenum) { this.user_password = user_password; this.user_phonenum = user_phonenum; } public User(int user_id, String user_name, String user_password, int user_pin, String user_sex, double user_wallet, String user_email, String user_phonenum) { this.user_id = user_id; this.user_name = user_name; this.user_password = user_password; this.user_pin = user_pin; this.user_sex = user_sex; this.user_wallet = user_wallet; this.user_email = user_email; this.user_phonenum = user_phonenum; } public void setUser_id(int user_id) { this.user_id = user_id; } public void setUser_name(String user_name) { this.user_name = user_name; } public void setUser_password(String user_password) { this.user_password = user_password; } public void setUser_pin(int user_pin) { this.user_pin = user_pin; } public void setUser_sex(String user_sex) { this.user_sex = user_sex; } public void setUser_wallet(double user_wallet) { this.user_wallet = user_wallet; } public void setUser_email(String user_email) { this.user_email = user_email; } public void setUser_phonenum(String user_phonenum) { this.user_phonenum = user_phonenum; } public int getUser_id() { return user_id; } public String getUser_name() { return user_name; } public String getUser_password() { return user_password; } public int getUser_pin() { return user_pin; } public String getUser_sex() { return user_sex; } public double getUser_wallet() { return user_wallet; } public String getUser_email() { return user_email; } public String getUser_phonenum() { return user_phonenum; } @Override public String toString() { return "User{" + "user_id=" + user_id + ", user_name='" + user_name + '\'' + ", user_password='" + user_password + '\'' + ", user_pin=" + user_pin + ", user_sex='" + user_sex + '\'' + ", user_wallet=" + user_wallet + ", user_email='" + user_email + '\'' + ", user_phonenum=" + user_phonenum + '}'; } }
3.在資料庫中有對應上面的模型類的表
4.在mapper中建立對應的UserMapper介面:
package com.bookShop.test.mapper;
import com.bookShop.test.model.User;
import java.util.List;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Param;
@Mapper
public interface UserMapper {
public User check_user(User user);
public User find_userById(int user_id);
public int update_password(User user);
public int add_user(User user);
public User find_byPhone(String user_phonenum);
//修改密碼
public int changepassword(User user);
//獲取地址
public List getaddress(User user);
//修改暱稱
public int saveusername(User user);
//修改支付密碼
public int changepin(User user);
public User queryByUserId(int user_id);
}
5.對應上面介面的方法 ,有對應的UserMapper.xml檔案,其中介面的方法名要和xml中每個方法的id相同,傳入的引數和返回結果的型別要一一對應
<?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="com.bookShop.test.mapper.UserMapper" >
<select id="queryByUserId" parameterType="int" resultType="com.bookShop.test.model.User">
select * from user where user_id = #{user_id}
</select>
<update id="update_password" parameterType="com.bookShop.test.model.User">
update user set user_password = #{user_password} where user_id = #{user_id}
</update>
<insert id="add_user" parameterType="com.bookShop.test.model.User">
insert into user(user_name,user_password,user_sex,user_phonenum) values (#{user_name},#{user_password},#{user_sex},#{user_phonenum})
</insert>
<select id="find_byPhone" parameterType="String" resultType="com.bookShop.test.model.User">
select * from user where user_phonenum = #{user_phonenum}
</select>
<update id="changepassword" parameterType="com.bookShop.test.model.User">
update user set user_password=#{user_password} where user_phonenum=#{user_phonenum}
</update>
<select id="getaddress" parameterType="com.bookShop.test.model.User" resultMap="Addresses">
select * from address where user_id=#{user_id}
</select>
<resultMap id="Addresses" type="com.bookShop.test.model.Address">
<result column="address_id" property="address_id"/>
<result column="address_name" property="address_name"/>
<result column="user_id" property="user_id"/>
<result column="accept_username" property="accept_username"/>
<result column="accept_userphone" property="accept_userphone"/>
</resultMap>
<update id="changepin" parameterType="com.bookShop.test.model.User">
update user set user_pin=#{user_pin} where user_phonenum=#{user_phonenum}
</update>
<update id="saveusername" parameterType="com.bookShop.test.model.User">
update user set user_name=#{user_name} where user_phonenum=#{user_phonenum}
</update>
<select id="check_user" parameterType="com.bookShop.test.model.User" resultType="com.bookShop.test.model.User">
select * from user where user_phonenum = #{user_phonenum} and user_password =#{user_password}
</select>
<select id="find_userById" parameterType="int" resultType="com.bookShop.test.model.User">
select * from user where user_id = #{user_id}
</select>
</mapper>
6.最後就可以通過spring的控制反轉(IOC)來呼叫一個UserMapper介面的例項的方法了,或者通過SqlSession來呼叫,下面 的這段程式碼是用的其他人的SqlSession程式碼:
//1、載入核心配置檔案
InputStream inputStream = Resources.getResourceAsStream("SqlMapConfig.xml");
//2、建立sqlSessionFactory
SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(inputStream);
//3、建立sqlSession
SqlSession sqlSession = sqlSessionFactory.openSession();
//4、執行sql語句,獲取結果(user)
User user = sqlSession.selectOne("getUserById", 1);
//5、列印結果
System.out.println(user);
//6.關閉sqlSession
sqlSession.close();
---------------------
作者:喵喵木子
來源:CSDN
原文:https://blog.csdn.net/qicui2835/article/details/80784850
版權宣告:本文為博主原創文章,轉載請附上博文連結!