Selaa lähdekoodia

mybatis change 3

zhangxiaoyu 2 vuotta sitten
vanhempi
commit
b9bb8e9fa0

+ 8 - 5
mybatis_train/src/main/resources/mapper/AccountMapper.xml

@@ -1,15 +1,18 @@
1
 <?xml version="1.0" encoding="UTF-8"?>
1
 <?xml version="1.0" encoding="UTF-8"?>
2
 <!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd">
2
 <!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd">
3
+
3
 <mapper namespace="com.mapper.AccountMapper">
4
 <mapper namespace="com.mapper.AccountMapper">
4
-    <select id="findAll" resultType="com.pojo.entity.AccountEntity"
5
+
6
+
7
+    <select id="findAll" resultType="account"
5
             parameterType="java.lang.Integer"
8
             parameterType="java.lang.Integer"
6
     >
9
     >
7
         select id,user_name,balance from account where id= #{id}
10
         select id,user_name,balance from account where id= #{id}
8
     </select>
11
     </select>
9
-    <update id="updateOne" parameterType="com.pojo.entity.AccountEntity">
10
-        update account set user_name = #{user_name} where id = #{id}
11
-    </update>
12
-    <insert id="insertOne" parameterType="com.pojo.entity.AccountEntity">
12
+    <insert id="insertOne" parameterType="account">
13
         insert into account(user_name,balance) value (#{user_name},#{balance})
13
         insert into account(user_name,balance) value (#{user_name},#{balance})
14
     </insert>
14
     </insert>
15
+    <update id="updateOne" parameterType="account">
16
+        update account set user_name= #{user_name},balance=#{balance} where id = #{id}
17
+    </update>
15
 </mapper>
18
 </mapper>

+ 9 - 1
mybatis_train/src/main/resources/mybatisConfig.xml

@@ -4,6 +4,10 @@
4
 <configuration>
4
 <configuration>
5
     <!--数据源的配置信息会单独放在jdbc.properties文件中,这里加载到mybatis配置文件来,使用的时候用EL表达式${}-->
5
     <!--数据源的配置信息会单独放在jdbc.properties文件中,这里加载到mybatis配置文件来,使用的时候用EL表达式${}-->
6
     <properties resource="jdbc.properties"></properties>
6
     <properties resource="jdbc.properties"></properties>
7
+    <!--    Mapper文件注册-->
8
+    <typeAliases>
9
+        <typeAlias type="com.pojo.entity.AccountEntity" alias="account"></typeAlias>
10
+    </typeAliases>
7
     <!--    核心配置信息-->
11
     <!--    核心配置信息-->
8
     <environments default="xmq_config">
12
     <environments default="xmq_config">
9
         <!--        数据库相关配置-->
13
         <!--        数据库相关配置-->
@@ -19,8 +23,12 @@
19
             </dataSource>
23
             </dataSource>
20
         </environment>
24
         </environment>
21
     </environments>
25
     </environments>
22
-    <!--    Mapper文件注册-->
26
+
23
     <mappers>
27
     <mappers>
28
+
24
         <mapper resource="mapper/AccountMapper.xml"/>
29
         <mapper resource="mapper/AccountMapper.xml"/>
30
+<!--        使用class方式引入-->
31
+<!--        <mapper class="com.mapper.AccountMapper"></mapper>-->
25
     </mappers>
32
     </mappers>
33
+
26
 </configuration>
34
 </configuration>

+ 26 - 0
mybatis_train/src/main/resources/sqlConfig.xml

@@ -0,0 +1,26 @@
1
+<?xml version="1.0" encoding="UTF-8"?>
2
+<!DOCTYPE configuration
3
+        PUBLIC "-//mybatis.org//DTD Config 3.0//EN"
4
+        "http://mybatis.org/dtd/mybatis-3-config.dtd" >
5
+
6
+<configuration>
7
+
8
+	<environments default="dev">
9
+		<environment id="dev">
10
+			<transactionManager type="JDBC"></transactionManager>
11
+			<dataSource type="POOLED">
12
+				<property name="driver" value="com.mysql.jdbc.Driver"/>
13
+				<property name="url" value="jdbc:mysql://localhost:3306/zhengshi?zeroDateTimeBehavior=convertToNull&amp;useUnicode=true&amp;autoReconnect=true&amp;characterEncoding=UTF-8&amp;serverTimezone=Asia/Shanghai"/>
14
+				<property name="username" value="root"/>
15
+				<property name="password" value=""/>
16
+			</dataSource>
17
+		</environment>
18
+	</environments>
19
+<!-- 加载映射文件-->
20
+	<mappers>
21
+		<mapper resource="mapper/AccountMapper.xml"></mapper>
22
+	</mappers>
23
+
24
+</configuration>
25
+
26
+

+ 15 - 12
mybatis_train/src/test/java/Test1.java

@@ -7,24 +7,27 @@ import org.junit.Test;
7
 
7
 
8
 import java.io.IOException;
8
 import java.io.IOException;
9
 import java.io.InputStream;
9
 import java.io.InputStream;
10
-import java.util.List;
11
 
10
 
12
 public class Test1 {
11
 public class Test1 {
13
 
12
 
14
     @Test
13
     @Test
15
     public void test() throws IOException {
14
     public void test() throws IOException {
16
         InputStream resourceAsStream = Resources.getResourceAsStream("mybatisConfig.xml");
15
         InputStream resourceAsStream = Resources.getResourceAsStream("mybatisConfig.xml");
17
-        SqlSessionFactory build = new SqlSessionFactoryBuilder().build(resourceAsStream);
18
-        SqlSession sqlSession = build.openSession();
19
-        List<AccountEntity> objects = sqlSession.selectList("com.mapper.AccountMapper.findAll",7);
20
-//        AccountEntity optEntity = new AccountEntity();
21
-//        optEntity.setId(7);
22
-//        optEntity.setUser_name("张7123");
23
-//        int update = sqlSession.update("com.mapper.AccountMapper.updateOne", optEntity);
24
-        AccountEntity addEntity = new AccountEntity();
25
-        addEntity.setUser_name("zz1");
26
-        addEntity.setBalance(27.5);
27
-        int insert = sqlSession.insert("com.mapper.AccountMapper.insertOne", addEntity);
16
+        SqlSessionFactory sqlSessionFactoryBuilder = new SqlSessionFactoryBuilder().build(resourceAsStream);
17
+        SqlSession sqlSession = sqlSessionFactoryBuilder.openSession();
18
+        // 查询
19
+    //    List<AccountEntity> objects = sqlSession.selectList("com.mapper.AccountMapper.findAll");
20
+        // 插入
21
+        AccountEntity accountEntity = new AccountEntity();
22
+        accountEntity.setUser_name("这");
23
+        accountEntity.setBalance(70.4);
24
+        sqlSession.insert("com.mapper.AccountMapper.insertOne",accountEntity);
25
+        AccountEntity updateEntity = new AccountEntity();
26
+        updateEntity.setId(9);
27
+        updateEntity.setUser_name("我是");
28
+        updateEntity.setBalance(100.0);
29
+        sqlSession.update("com.mapper.AccountMapper.updateOne",updateEntity);
30
+        // mybatis 如果执行了更新操作需要提交事务
28
         sqlSession.commit();
31
         sqlSession.commit();
29
         sqlSession.close();
32
         sqlSession.close();
30
     }
33
     }