MyBatis妗嗘灦鍏ラ棬

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了MyBatis妗嗘灦鍏ラ棬相关的知识,希望对你有一定的参考价值。

鏍囩锛?a href='http://www.mamicode.com/so/1/junit' title='junit'>junit   session   source   鏁版嵁   delete   ali   lap   淇敼   git   

MyBatis鏋舵瀯涓庨」鐩惌寤?/h3>

鎶€鏈浘鐗? src=鎶€鏈浘鐗? src=

鈼?瀵煎叆Jar

鏁版嵁搴撻┍鍔ㄥ寘锛?span style="color: #0000ff;">mysql-connector-java-5.1.46-bin.jar

鎶€鏈浘鐗? src=

mybatis鏍稿績鍖咃細mybatis-3.4.6.jar

鎶€鏈浘鐗? src= 

mybatis渚濊禆锛?/p>

鎶€鏈浘鐗? src=

鈼?鍑嗗鏁版嵁搴?/strong>

鎶€鏈浘鐗? src=

鎶€鏈浘鐗? src=

鈼?db.properties - mysql椹卞姩閰嶇疆鏂囦欢锛堣繖鏍峰彲浠ヤ紭鍖栨€ц兘锛?/p>

鎶€鏈浘鐗? id=
jdbc.driver=com.mysql.jdbc.Driver
jdbc.url=jdbc:mysql://localhost:3306/games_db?useUnicode=true&characterEncoding=UTF-8&useSSL=true
jdbc.username=root
jdbc.password=123456
View Code

鈼?寮曞叆log4j.properties鏂囦欢锛堟祴璇曡緭鍑洪渶瑕侊級

鈼?娣诲姞mybatis鏄犲皠閰嶇疆鏂囦欢 - sqlMapConfig.xml

閰嶇疆鏂囦欢涓富瑕佹槸閰嶇疆杩炴帴鏁版嵁搴撳拰浜嬪姟绠$悊鐨勫唴瀹癸紝鏂囦欢鍚嶅彲浠ヨ嚜瀹氫箟锛岄粯璁qlMapConfig.xml

<?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"></properties>
    <!-- 閰嶇疆鍒悕;鎺ㄨ崘浣跨敤package鍖呭舰寮忔潵閰嶇疆鍒悕;鍖呯殑褰㈠紡浼氭壂鎻忎富鍖呭強瀛愬寘涓墍鏈夋枃浠?浠ュ璞$被鍚嶄负鍒悕,澶у皬鍐欎笉闄?鎺ㄨ崘浣跨敤灏忓啓 -->
    <typeAliases>
        <!-- <typeAlias type="com.sikiedu.beans.User" alias="user" /> -->
        <package name="com.sikiedu.beans" />
    </typeAliases>
    <!-- 閰嶇疆mybatis杩愯鐜 ; 鍦ㄩ泦鎴怱pring鏄笉鐢?-->
    <environments default="development">
        <environment id="development">
            <!-- type="JDBC" 浠h〃浣跨敤JDBC鐨勬彁浜ゅ拰鍥炴粴鏉ョ鐞嗕簨鍔?-->
            <transactionManager type="JDBC" />
            <!-- 浣跨敤杩炴帴姹犺繛鎺ユ暟鎹簱 - mybatis鎻愪緵浜?绉嶆暟鎹簮绫诲瀷锛屽垎鍒槸锛歅OOLED,UNPOOLED,JNDI -->
            <!-- POOLED 琛ㄧず鏀寔JDBC鏁版嵁婧愯繛鎺ユ睜 ; UNPOOLED 琛ㄧず涓嶆敮鎸佹暟鎹簮杩炴帴姹?; JNDI 琛ㄧず鏀寔澶栭儴鏁版嵁婧愯繛鎺ユ睜 -->
            <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="com/sikiedu/mapper/UserMapper.xml" /> -->
        <!-- <mapper url="file:D:JavaWorkSpaceJavaEE_SSM_Testssm_mybatissrccomsikiedumapperUserMapper.xml" /> -->
        <!-- <mapper class="com.sikiedu.mapper.UserMapper" /> -->
        <!-- 澶氶厤缃?浠ュ寘鐨勫舰寮?鎺ㄨ崘浣跨敤 -->
        <package name="com.sikiedu.mapper" />
    </mappers>
</configuration>

鈼?鍒涘缓鏁版嵁搴撳搴旂殑瀹炰綋瀵硅薄

鎶€鏈浘鐗? src=

鏁版嵁搴撳疄浣撳璞?/p>

鎶€鏈浘鐗? id=
 1 package com.sikiedu.beans;
 2 
 3 public class Role {
 4 
 5     private Integer id;
 6     private String name;
 7     private Integer level;
 8     private String roletype;
 9     private Integer userid;
10 
11     public Integer getId() {
12         return id;
13     }
14 
15     public void setId(Integer id) {
16         this.id = id;
17     }
18 
19     public String getName() {
20         return name;
21     }
22 
23     public void setName(String name) {
24         this.name = name;
25     }
26 
27     public Integer getLevel() {
28         return level;
29     }
30 
31     public void setLevel(Integer level) {
32         this.level = level;
33     }
34 
35     public String getRoletype() {
36         return roletype;
37     }
38 
39     public void setRoletype(String roletype) {
40         this.roletype = roletype;
41     }
42 
43     public Integer getUserid() {
44         return userid;
45     }
46 
47     public void setUserid(Integer userid) {
48         this.userid = userid;
49     }
50 
51     @Override
52     public String toString() {
53         return "Role [id=" + id + ", name=" + name + ", level=" + level + ", roletype=" + roletype + ", userid="
54                 + userid + "]";
55     }
56 
57 }
Role.java
鎶€鏈浘鐗? id=
 1 package com.sikiedu.beans;
 2 
 3 import java.util.Date;
 4 
 5 public class User {
 6 
 7     private Integer id;
 8     private String username;
 9     private String userpassword;
10     private Float balance;
11     private String grgisterdate;
12 
13     public Integer getId() {
14         return id;
15     }
16 
17     public void setId(Integer id) {
18         this.id = id;
19     }
20 
21     public String getUsername() {
22         return username;
23     }
24 
25     public void setUsername(String username) {
26         this.username = username;
27     }
28 
29     public String getUserpassword() {
30         return userpassword;
31     }
32 
33     public void setUserpassword(String userpassword) {
34         this.userpassword = userpassword;
35     }
36 
37     public Float getBalance() {
38         return balance;
39     }
40 
41     public void setBalance(Float balance) {
42         this.balance = balance;
43     }
44 
45     public String getGrgisterdate() {
46         return grgisterdate;
47     }
48 
49     public void setGrgisterdate(String grgisterdate) {
50         this.grgisterdate = grgisterdate;
51     }
52 
53     @Override
54     public String toString() {
55         return "User [id=" + id + ", username=" + username + ", userpassword=" + userpassword + ", balance=" + balance
56                 + ", grgisterdate=" + grgisterdate + "]";
57     }
58 
59 }
User.java

Vo涓哄叧鑱旀煡璇㈤渶瑕?- 涓€瀵逛竴銆佷竴瀵瑰

鎶€鏈浘鐗? id=
 1 package com.sikiedu.beans;
 2 
 3 import java.util.List;
 4 
 5 public class RoleVo extends Role {
 6 
 7     private User user;
 8     private List<Integer> idList;
 9 
10     public User getUser() {
11         return user;
12     }
13 
14     public void setUser(User user) {
15         this.user = user;
16     }
17 
18     public List<Integer> getList() {
19         return idList;
20     }
21 
22     public void setList(List<Integer> idList) {
23         this.idList = idList;
24     }
25 
26     @Override
27     public String toString() {
28         return "RoleVo [ID=" + getId() + "	 name=" + getName() + "	 roletype=" + getRoletype() + "	 user=" + user
29                 + "]";
30     }
31 
32 }
RoleVo.java
鎶€鏈浘鐗? id=
 1 package com.sikiedu.beans;
 2 
 3 import java.util.List;
 4 
 5 public class UserVo extends User {
 6     // 缁存姢涓€涓猂ole闆嗗悎
 7     private List<Role> roleList;
 8 
 9     public List<Role> getRole() {
10         return roleList;
11     }
12 
13     public void setRole(List<Role> roleList) {
14         this.roleList = roleList;
15     }
16 
17     @Override
18     public String toString() {
19         return "UserVo [ID=" + getId() + "	 username=" + getUsername() + "	 Grgisterdate=" + getGrgisterdate()
20                 + "	 role=" + roleList + "]";
21     }
22 
23 }
UserVo.java

鈼?鍒涘缓鏂规硶鎺ュ彛XxxxMapper.java And 瀹氫箟鎿嶄綔鏁版嵁琛ㄧ殑sql鏄犲皠鏂囦欢XxxMapper.xml

鎶€鏈浘鐗? src=

鍒涘缓鏂规硶鎺ュ彛 - 鎻愪緵绠€鍗曠殑澧炲垹鏀规煡鏁版嵁淇℃伅銆?/p>

鎶€鏈浘鐗? id=
 1 package com.sikiedu.mapper;
 2 
 3 import java.util.List;
 4 
 5 import com.sikiedu.beans.Role;
 6 import com.sikiedu.beans.RoleVo;
 7 
 8 public interface RoleMapper {
 9 
10     // 鏌ヨ鎵€鏈?/span>
11     public List<Role> selectAllRole();
12 
13     // 閫氳繃RoleVo涓€瀵逛竴鍏宠仈鏌ヨ
14     public List<RoleVo> selectAllRoleVo();
15 
16     // 澶氭潯浠舵煡璇?閫氳繃瑙掕壊瀵硅薄涓殑鏉′欢鏌ヨ瑙掕壊琛?/span>
17     public List<Role> selectRoleListByRole(Role role);
18 
19     // 澶氭潯浠舵煡璇?閫氳繃瑙掕壊瀵硅薄涓殑鏉′欢鏌ヨ瑙掕壊琛═rim
20     public List<Role> selectRoleListByTrim(Role role);
21 
22     // 鏇存柊瑙掕壊琛?/span>
23     public void updateSetRole(Role role);
24 
25     // 浣跨敤澶氫釜ID鑾峰彇瑙掕壊鍒楄〃 By array
26     public List<Role> selectRoleListByids(Integer[] ids);
27 
28     // 浣跨敤澶氫釜ID鑾峰彇瑙掕壊鍒楄〃 By list
29     public List<Role> selectRoleListByList(List<Integer> idList);
30 
31     // 浣跨敤澶氫釜ID鑾峰彇瑙掕壊鍒楄〃 By RoleVo
32     public List<Role> selectRoleListByRoleVo(RoleVo roleVo);
33 
34 }
RoleMapper.java
鎶€鏈浘鐗? id=
 1 package com.sikiedu.mapper;
 2 
 3 import java.util.List;
 4 
 5 import com.sikiedu.beans.User;
 6 import com.sikiedu.beans.UserVo;
 7 
 8 public interface UserMapper {
 9 
10     // 鏍规嵁ID鏌ヨ鐢ㄦ埛
11     public User selectUserById(Integer id);
12 
13     // 鏍规嵁鐢ㄦ埛鍚嶆ā绯婃煡璇㈢敤鎴?/span>
14     public List<User> selectUserLikeUsername(String username);
15 
16     // 鍚岃繃鍖呰绫籙serVo鏌ヨ鐢ㄦ埛
17     public User selectUserByUserVoId(UserVo vo);
18 
19     // 鏌ヨ鐢ㄦ埛鎬绘潯鏁?/span>
20     public Integer selectUserCount();
21 
22     // 鏌ヨ鎵€鏈夌敤鎴峰寘瑁呯被
23     public List<UserVo> selectAllUserVo();
24 }
UserMapper.java

瀹氫箟鎿嶄綔鏁版嵁琛ㄧ殑sql鏄犲皠鏂囦欢

鎶€鏈浘鐗? id=
  1 <?xml version="1.0" encoding="UTF-8" ?>
  2 <!DOCTYPE mapper
  3   PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
  4   "http://mybatis.org/dtd/mybatis-3-mapper.dtd">
  5 <mapper namespace="com.sikiedu.mapper.RoleMapper">
  6 
  7     <sql id="myselect">
  8         SELECT *
  9         FROM role
 10     </sql>
 11 
 12     <resultMap type="Role" id="roleRM">
 13         <id property="id" column="idrole" />
 14     </resultMap>
 15     <select id="selectAllRole" resultMap="roleRM">
 16         <include refid="myselect"></include>
 17     </select>
 18 
 19     <resultMap type="RoleVo" id="roleVo">
 20         <id property="id" column="idrole" />
 21         <result property="name" column="name" />
 22         <result property="level" column="level" />
 23         <result property="roletype" column="roletype" />
 24         <!-- 涓€瀵逛竴鍏崇郴 -->
 25         <association property="user" javaType="User">
 26             <id property="id" column="id" />
 27             <result property="username" column="username" />
 28         </association>
 29     </resultMap>
 30     <!-- 涓€瀵逛竴鍏宠仈鏌ヨ -->
 31     <select id="selectAllRoleVo" resultMap="roleVo">
 32         SELECT
 33         r.idrole,
 34         r.name,
 35         r.level,
 36         r.roletype,
 37         u.id,
 38         u.username
 39         FROM role r
 40         LEFT JOIN user u
 41         ON r.idrole = u.id
 42     </select>
 43 
 44     <!-- 澶氭潯浠舵煡璇?閫氳繃瑙掕壊瀵硅薄涓殑鏉′欢鏌ヨ瑙掕壊琛?-->
 45     <select id="selectRoleListByRole" parameterType="Role" resultMap="roleRM">
 46         <include refid="myselect"></include>
 47         <where>
 48             <if test="level!=null and level!=鈥樷€?>
 49                 level = #{level}
 50             </if>
 51             <if test="roletype!=null and roletype!=鈥樷€?>
 52                 AND roletype = #{roletype}
 53             </if>
 54             <if test="userid!=null">
 55                 AND userid = #{userid}
 56             </if>
 57         </where>
 58     </select>
 59 
 60     <!--澶氭潯浠舵煡璇?閫氳繃瑙掕壊瀵硅薄涓殑鏉′欢鏌ヨ瑙掕壊琛═rim -->
 61     <select id="selectRoleListByTrim" parameterType="Role" resultMap="roleRM">
 62         <include refid="myselect"></include>
 63         <trim prefix="WHERE" suffixOverrides="AND">
 64             <if test="level!=null and level!=鈥樷€?>
 65                 level = #{level} AND
 66             </if>
 67             <if test="roletype!=null and roletype!=鈥樷€?>
 68                 roletype = #{roletype} AND
 69             </if>
 70             <if test="userid!=null">
 71                 userid = #{userid}
 72             </if>
 73         </trim>
 74     </select>
 75 
 76     <!-- 鏇存柊瑙掕壊琛?-->
 77     <update id="updateSetRole" parameterType="Role">
 78         UPDATE role
 79         <set>
 80             <if test="name != null and name != 鈥樷€?>
 81                 name = 鈥?{name}鈥?
 82             </if>
 83             <if test="level != null">
 84                 level = ${level},
 85             </if>
 86             <if test="roletype != null and roletype != 鈥樷€?>
 87                 roletype = 鈥?{roletype}鈥?
 88             </if>
 89             <if test="userid != null">
 90                 userid = ${userid}
 91             </if>
 92         </set>
 93         WHERE
 94         idrole = #{id}
 95     </update>
 96 
 97     <!-- 浣跨敤澶氫釜ID鑾峰彇瑙掕壊鍒楄〃 foreach閬嶅巻鏁扮粍 -->
 98     <select id="selectRoleListByids" resultMap="roleRM">
 99         <include refid="myselect"></include>
100         WHERE idrole IN
101         <foreach collection="array" item="id" open="(" separator="," close=")">
102             ${id}
103         </foreach>
104     </select>
105 
106     <!-- 浣跨敤澶氫釜ID鑾峰彇瑙掕壊鍒楄〃 foreach閬嶅巻闆嗗悎 -->
107     <select id="selectRoleListByList" resultMap="roleRM">
108         <include refid="myselect"></include>
109         WHERE idrole IN
110         <foreach collection="list" item="id" open="(" separator="," close=")">
111             ${id}
112         </foreach>
113     </select>
114 
115     <select id="selectRoleListByRoleVo" resultMap="roleRM">
116         <include refid="myselect"></include>
117         WHERE idrole IN
118         <foreach collection="list" item="id" open="(" separator="," close=")">
119             ${id}
120         </foreach>
121     </select>
122     
123 </mapper>
RoleMapper.xml
鎶€鏈浘鐗? id=
 1 <?xml version="1.0" encoding="UTF-8" ?>
 2 <!DOCTYPE mapper
 3   PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
 4   "http://mybatis.org/dtd/mybatis-3-mapper.dtd">
 5 <mapper namespace="com.sikiedu.mapper.UserMapper">
 6 
 7     <!-- 鏌ヨ鐢ㄦ埛 -->
 8     <select id="selectUserById" parameterType="Integer" resultType="com.sikiedu.beans.User">
 9         SELECT * FROM user WHERE id = #{id}
10     </select>
11     <!-- #{}鍗犱綅绗?灏介噺閫夌敤#{}鏉ヨВ鍐抽棶棰?-->
12     <!-- ${}瀛楃鎷兼帴 or1=1 瀹规槗琚玈QL娉ㄥ叆 -->
13     <select id="selectUserLikeUsername" parameterType="String" resultType="User">
14         <!-- SELECT * FROM coke.user WHERE username LIKE 鈥?${value}%鈥?-->
15         SELECT * FROM user WHERE username LIKE "%"#{username}"%"
16     </select>
17 
18     <!-- 娣诲姞鐢ㄦ埛 -->
19     <insert id="insertUser" parameterType="com.sikiedu.beans.User">
20         INSERT INTO user VALUES(null,#{username},#{userpassword},#{balance},#{grgisterdate})
21     </insert>
22 
23     <!-- 淇敼鐢ㄦ埛 -->
24     <update id="updateUser" parameterType="com.sikiedu.beans.User">
25         UPDATE user SET username = #{username} WHERE id = #{id}
26     </update>
27 
28     <!-- 鍒犻櫎鐢ㄦ埛 -->
29     <delete id="deleteUserById" parameterType="Integer">
30         DELETE FROM user WHERE id = #{id}
31     </delete>
32 
33     <!-- 鍚岃繃鍖呰绫籙serVo鏌ヨ鐢ㄦ埛 -->
34     <select id="selectUserByUserVoId" parameterType="UserVo" resultType="user">
35         SELECT * FROM user WHERE id = #{id}
36     </select>
37 
38     <!-- selectUserCount -->
39     <select id="selectUserCount" resultType="Integer">
40         SELECT COUNT(*) FROM user
41     </select>
42 
43     <resultMap type="UserVo" id="userVo">
44         <id property="id" column="id" />
45         <result property="username" column="username" />
46         <result property="grgisterdate" column="grgisterdate" />
47         <!-- 涓€瀵瑰鍏崇郴 -->
48         <collection property="roleList" ofType="Role">
49             <id property="id" column="idrole" />
50             <result property="name" column="name" />
51             <result property="roletype" column="roletype" />
52         </collection>
53     </resultMap>
54     <!-- 涓€瀵瑰鍏宠仈鏌ヨ -->
55     <select id="selectAllUserVo" resultMap="userVo">
56         SELECT
57         u.id,
58         u.username,
59         u.grgisterdate,
60         r.idrole,
61         r.name,
62         r.roletype
63         FROM user u
64         LEFT JOIN role r
65         ON u.id = r.userid
66     </select>
67 
68 </mapper>
UserMapper.xml

鏄犲皠閰嶇疆鏂囦欢閲屾敞鍐屾槧灏勬枃浠?/span>

1 <!--閰嶇疆鏄犲皠鍣ㄧ殑浣嶇疆 -->
2 <mappers>
3     <!-- 鍗曚釜閰嶇疆 -->
4     <!-- <mapper resource="com/sikiedu/mapper/UserMapper.xml" /> -->
5     <!-- <mapper url="file:D:JavaWorkSpaceJavaEE_SSM_Testssm_mybatissrccomsikiedumapperUserMapper.xml" /> -->
6     <!-- <mapper class="com.sikiedu.mapper.UserMapper" /> -->
7     <!-- 鑷姩鎵弿鍖呭唴鐨凪apper鎺ュ彛涓庨厤缃枃浠?- 浠ュ寘鐨勫舰寮?鎺ㄨ崘浣跨敤 -->
8     <package name="com.sikiedu.mapper" />
9 </mappers>

娴嬭瘯 - Dao鍘熷寮€鍙戞柟寮?/p>

鎶€鏈浘鐗? id=
1 package com.sikiedu.dao;
2 
3 import com.sikiedu.beans.User;
4 
5 public interface UserDao {
6 
7     public User getUserById(Integer id);
8 
9 }
UserDao.java
鎶€鏈浘鐗? id=
 1 package com.sikiedu.dao;
 2 
 3 import org.apache.ibatis.session.SqlSession;
 4 import org.apache.ibatis.session.SqlSessionFactory;
 5 
 6 import com.sikiedu.beans.User;
 7 
 8 public class UserDaoImpl implements UserDao {
 9 
10     // SQLSession宸ュ巶
11     private SqlSessionFactory ssf;
12 
13     // 閫氳繃鏋勯€犵粰ssf璧嬪€?/span>
14     public UserDaoImpl(SqlSessionFactory ssf) {
15         super();
16         this.ssf = ssf;
17     }
18 
19     @Override
20     public User getUserById(Integer id) {
21         // 鐢熶骇涓€涓猻ession
22         SqlSession sqlSession = ssf.openSession();
23         // 鎿嶄綔鏁版嵁搴?/span>
24         return sqlSession.selectOne("selectUserById", id);
25     }
26 
27 }
UserDaoImpl.java
鎶€鏈浘鐗? id=
 1 package com.sikiedu.test;
 2 
 3 import java.io.IOException;
 4 import java.io.InputStream;
 5 
 6 import org.apache.ibatis.io.Resources;
 7 import org.apache.ibatis.session.SqlSessionFactory;
 8 import org.apache.ibatis.session.SqlSessionFactoryBuilder;
 9 import org.junit.Test;
10 
11 import com.sikiedu.beans.User;
12 import com.sikiedu.dao.UserDaoImpl;
13 
14 public class UserDaoTest {
15 
16     // sqlSession宸ュ巶
17     private static SqlSessionFactory ssf;
18 
19     static {
20         String resource = "sqlMapConfig.xml";
21         try {
22             // 璇诲彇閰嶇疆鏂囦欢
23             InputStream inputStream = Resources.getResourceAsStream(resource);
24             // 鍒涘缓sqlSession宸ュ巶
25             ssf = new SqlSessionFactoryBuilder().build(inputStream);
26         } catch (IOException e) {
27             e.printStackTrace();
28         }
29     }
30 
31     @Test // 鏍规嵁ID鏌ヨ鐢ㄦ埛
32     public void DaoTest() {
33 
34         UserDaoImpl dao = new UserDaoImpl(ssf);
35         User user = dao.getUserById(1);
36         System.out.println(user);
37     }
38 }
UserDaoTest.java

澧炲垹鏀规煡鎿嶄綔

璇诲彇閰嶇疆鏂囦欢锛屽垱寤簊qlSessionFactory锛岃幏鍙杝qlSession

// 璇诲彇閰嶇疆鏂囦欢
String resource = "sqlMapConfig.xml";
InputStream in = Resources.getResourceAsStream(resource);

// 闇€瑕乻qlSessionFactoryBuilder
SqlSessionFactoryBuilder ssfb = new SqlSessionFactoryBuilder();

// 鍒涘缓sqlSessionFactory
SqlSessionFactory ssf = ssfb.build(in);

// 鐢熶骇涓€涓猻qlSession
SqlSession sqlSession = ssf.openSession();

♦ 澧?/p>

<insert id="insertUser" parameterType="com.sikiedu.beans.User">
    INSERT INTO user VALUES(null,#{username},#{userpassword},#{balance},#{grgisterdate})
</insert>
User user = new User();
user.setUsername("澶╃尗");
user.setUserpassword("123456");
user.setBalance(Float.valueOf(1000));
user.setGrgisterdate(new SimpleDateFormat("yyyy-MM-dd").format(new Date(System.currentTimeMillis())));
sqlSession.insert("com.sikiedu.mapper.UserMapper.insertUser", user);
sqlSession.commit();

♦ 鍒?/p>

<delete id="deleteUserById" parameterType="Integer">
    DELETE FROM user WHERE id = #{id}
</delete>
sqlSession.delete("com.sikiedu.mapper.UserMapper.deleteUserById", 100);
sqlSession.commit();

♦ 鏀?/p>

<update id="updateUser" parameterType="com.sikiedu.beans.User">
    UPDATE user SET username = #{username} WHERE id = #{id}
</update>
User user = new User();
user.setId(44);
user.setUsername("闃跨緤");
sqlSession.update("com.sikiedu.mapper.UserMapper.updateUser", user);
sqlSession.commit();

♦ 鏌?/p>

<!-- #{}鍗犱綅绗?灏介噺閫夌敤#{}鏉ヨВ鍐抽棶棰?-->
<!-- ${}瀛楃鎷兼帴 or1=1 瀹规槗琚玈QL娉ㄥ叆 -->
<select id="selectUserById" parameterType="Integer" resultType="com.sikiedu.beans.User">
    SELECT * FROM user WHERE id = #{id}
</select>
<select id="selectUserLikeUsername" parameterType="String" resultType="User">
    <!-- SELECT * FROM coke.user WHERE username LIKE 鈥?${value}%鈥?-->
    SELECT * FROM user WHERE username LIKE "%"#{username}"%"
</select>
// 鏌ヨ鐢ㄦ埛
// 鎿嶄綔鏁版嵁搴?鍙傛暟1-瑕佹搷浣滅殑sql璇彞锛涘弬鏁?-sql璇彞鍙傛暟
User user = sqlSession.selectOne("com.sikiedu.mapper.UserMapper.selectUserById", 3);
System.out.println(user);

// 閫氳繃username妯$硦 鏌ヨ鐢ㄦ埛
List<User> users = sqlSession.selectList("com.sikiedu.mapper.UserMapper.selectUserLikeUsername", "澶?);
for (User user : users) {
    System.out.println(user);
}

以上是关于MyBatis妗嗘灦鍏ラ棬的主要内容,如果未能解决你的问题,请参考以下文章

React Status 涓枃鍛ㄥ垔 #39 - 浜斿ぇ React 妗嗘灦娴嬭瘎

mybatis鍏ラ棬

webpack鍏ラ棬-涓汉瀛︿範璧勬簮鏀堕泦

Java鍏ラ棬(鍩虹绡囦竴)

鍊嶅鍏ラ棬

JavaPoet鍏ラ棬