浏览代码

mybatis project change3

zhang 2 年之前
父节点
当前提交
f60af65830

+ 3 - 1
mybatis_train/src/main/java/com/mapper/AccountMapper.java

@@ -1,11 +1,13 @@
1 1
 package com.mapper;
2 2
 
3 3
 import com.pojo.entity.AccountEntity;
4
+import org.apache.ibatis.annotations.Mapper;
5
+import org.springframework.stereotype.Component;
4 6
 import org.springframework.stereotype.Repository;
5 7
 
6 8
 import java.util.List;
7 9
 
8
-@Repository
10
+@Mapper
9 11
 public interface AccountMapper {
10 12
     List<AccountEntity> findAll(Integer id);
11 13
     void updateOne(AccountEntity accountEntity);

+ 7 - 0
mybatis_train/src/main/java/com/service/impl/AccountServiceImpl.java

@@ -0,0 +1,7 @@
1
+package com.service.impl;
2
+
3
+import org.springframework.stereotype.Service;
4
+
5
+@Service
6
+public class AccountServiceImpl {
7
+}

+ 23 - 0
mybatis_train/src/main/resources/SpringConfig.xml

@@ -0,0 +1,23 @@
1
+<?xml version="1.0" encoding="UTF-8"?>
2
+
3
+<beans xmlns="http://www.springframework.org/schema/beans"
4
+       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
5
+       xmlns:context="http://www.springframework.org/schema/context"
6
+       xsi:schemaLocation="http://www.springframework.org/schema/beans
7
+       http://www.springframework.org/schema/beans/spring-beans.xsd
8
+       http://www.springframework.org/schema/context
9
+       https://www.springframework.org/schema/context/spring-context.xsd">
10
+    <!--    <bean id="userService1" class="com.service.impl.UserServiceImpl">-->
11
+    <!--&lt;!&ndash;        <constructor-arg name="userDao" ref="UserDao"></constructor-arg>&ndash;&gt;-->
12
+    <!--    </bean>-->
13
+    <!--    加载外部 properties文件-->
14
+    <!--    <context:property-placeholder location="jdbc.properties"></context:property-placeholder>-->
15
+    <!--    <bean id="dataSource" class="com.alibaba.druid.pool.DruidDataSource">-->
16
+    <!--        <property name="driverClassName" value="${jdbc.driver}"></property>-->
17
+    <!--        <property name="url" value="${jdbc.url}"></property>-->
18
+    <!--        <property name="username" value="${jdbc.username}"></property>-->
19
+    <!--        <property name="password" value="${jdbc.password}"></property>-->
20
+    <!--    </bean>-->
21
+    <!--    组件扫描-->
22
+    <context:component-scan base-package="com"></context:component-scan>
23
+</beans>

+ 0 - 2
mybatis_train/src/main/resources/mapper/AccountMapper.xml

@@ -2,8 +2,6 @@
2 2
 <!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd">
3 3
 
4 4
 <mapper namespace="com.mapper.AccountMapper">
5
-
6
-
7 5
     <select id="findAll" resultType="account"
8 6
             parameterType="java.lang.Integer"
9 7
     >

+ 9 - 7
mybatis_train/src/test/java/Test1.java

@@ -14,21 +14,23 @@ public class Test1 {
14 14
     public void test() throws IOException {
15 15
         InputStream resourceAsStream = Resources.getResourceAsStream("mybatisConfig.xml");
16 16
         SqlSessionFactory sqlSessionFactoryBuilder = new SqlSessionFactoryBuilder().build(resourceAsStream);
17
-        SqlSession sqlSession = sqlSessionFactoryBuilder.openSession();
17
+        SqlSession sqlSession = sqlSessionFactoryBuilder.openSession(true);
18 18
         // 查询
19 19
     //    List<AccountEntity> objects = sqlSession.selectList("com.mapper.AccountMapper.findAll");
20 20
         // 插入
21
-        AccountEntity accountEntity = new AccountEntity();
22
-        accountEntity.setUser_name("这");
23
-        accountEntity.setBalance(70.4);
24
-        sqlSession.insert("com.mapper.AccountMapper.insertOne",accountEntity);
21
+//        AccountEntity accountEntity = new AccountEntity();
22
+//        accountEntity.setUser_name("这");
23
+//        accountEntity.setBalance(70.4);
24
+//        sqlSession.insert("com.mapper.AccountMapper.insertOne",accountEntity);
25 25
         AccountEntity updateEntity = new AccountEntity();
26 26
         updateEntity.setId(9);
27 27
         updateEntity.setUser_name("我是");
28 28
         updateEntity.setBalance(100.0);
29
-        sqlSession.update("com.mapper.AccountMapper.updateOne",updateEntity);
29
+         // namespace + functionName
30
+        sqlSession.update("accountMapper.updateOne",updateEntity);
30 31
         // mybatis 如果执行了更新操作需要提交事务
31
-        sqlSession.commit();
32
+//        sqlSession.commit();
33
+        sqlSession.rollback();
32 34
         sqlSession.close();
33 35
     }
34 36
 }

+ 16 - 0
mybatis_train/src/test/java/Test2.java

@@ -0,0 +1,16 @@
1
+import com.mapper.AccountMapper;
2
+import com.service.impl.AccountServiceImpl;
3
+import org.junit.Test;
4
+import org.springframework.context.annotation.AnnotationConfigApplicationContext;
5
+import org.springframework.context.support.ClassPathXmlApplicationContext;
6
+
7
+public class Test2 {
8
+    @Test
9
+    public void testDemo() {
10
+//        ClassPathXmlApplicationContext classPathXmlApplicationContext =
11
+//                new ClassPathXmlApplicationContext("SpringConfig.xml");
12
+        AnnotationConfigApplicationContext annotationConfigApplicationContext = new AnnotationConfigApplicationContext();
13
+        annotationConfigApplicationContext.refresh();
14
+        AccountServiceImpl bean = annotationConfigApplicationContext.getBean(AccountServiceImpl.class);
15
+    }
16
+}