1. Mybatis简单操作

0. pom.jar

<!-- https://mvnrepository.com/artifact/org.mybatis/mybatis -->
        <dependency>
            <groupId>org.mybatis</groupId>
            <artifactId>mybatis</artifactId>
            <version>3.5.3</version>
        </dependency>

 

1. mapper.xml

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.xing.mapper.AccountMapper">
6     <delete id="deleteAll">
7         delete from `account`;
8     </delete>
9 </mapper>

2. SqlMapConfig.xml

 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 <configuration>
 6     <environments default="development">
 7         <environment id="development">
 8             <transactionManager type="JDBC" />
 9             <dataSource type="POOLED">
10                 <property name="driver" value="com.mysql.jdbc.Driver" />
11                 <property name="url" value="jdbc:mysql:///student" />
12                 <property name="username" value="root" />
13                 <property name="password" value="123" />
14             </dataSource>
15         </environment>
16     </environments>
17     <mappers>
18         <!-- 加载sql语句的配置文件 -->
19         <mapper resource="mapper.xml"/>
20     </mappers>
21 </configuration>

 

3. Interface AccountMapper 

 1 package com.xing.mapper;
 2 import com.xing.beans.Account;
 3 import java.util.List;
 4 public interface AccountMapper {
 5     void deleteAll();
 6     List<Account> findAll();
 7     void deleteById( Integer id);
 8     Account findById(Integer id);
 9     double findMoneyById(Integer id);
10 }

4. Class Test

 1 package com.xing.test2;
 2 
 3 import com.xing.beans.Account;
 4 import com.xing.mapper.AccountMapper;
 5 import org.apache.ibatis.io.Resources;
 6 import org.apache.ibatis.session.SqlSession;
 7 import org.apache.ibatis.session.SqlSessionFactory;
 8 import org.apache.ibatis.session.SqlSessionFactoryBuilder;
 9 import org.junit.Test;
10 
11 import java.io.IOException;
12 import java.util.List;
13 
14 public class Test1 {
15     @Test
16     public void te1() throws IOException {
17         SqlSession sqlSession = new SqlSessionFactoryBuilder().build(Resources.getResourceAsStream("SqlMapConfig.xml")).openSession();
18         AccountMapper mapper = sqlSession.getMapper(AccountMapper.class);
19         mapper.deleteById(2);
20         sqlSession.commit();
21     }
22 
23     @Test
24     public void test2() throws IOException {
25         SqlSession sqlSession = new SqlSessionFactoryBuilder().build(Resources.getResourceAsStream("SqlMapConfig.xml")).openSession();
26         AccountMapper mapper = sqlSession.getMapper(AccountMapper.class);
27         List<Account> all = mapper.findAll();
28 29 }

注意: 

  1. 在做查询操作时,配置 mapper.xml 里 返回值类型 写为 对象不用写集合,当查询返回是对象集合时,会自动的转换的

 

原文地址:https://www.cnblogs.com/iscurry/p/11884880.html