|
@@ -7,24 +7,27 @@ import org.junit.Test;
|
7
|
7
|
|
8
|
8
|
import java.io.IOException;
|
9
|
9
|
import java.io.InputStream;
|
10
|
|
-import java.util.List;
|
11
|
10
|
|
12
|
11
|
public class Test1 {
|
13
|
12
|
|
14
|
13
|
@Test
|
15
|
14
|
public void test() throws IOException {
|
16
|
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
|
31
|
sqlSession.commit();
|
29
|
32
|
sqlSession.close();
|
30
|
33
|
}
|