MyBatis下载、配置及测试
点击下载
MyBatis在线中文文档
下载之后打开,如图:
第一个使我们需要用到的包,pdf文档是MyBatis英文手册,后面两个分别是javadoc文档和源码。
在这里我们还需要导入MySQL数据库驱动jar包。官方驱动jar包下载地址
这些准备以后,我们开始配置。
- 打开MyEclipse,导入jar包。在src下新建一个包,并新建配置文件conf.xml。如图:
新建测试数据库mybatis,并新建两条数据。
在Mybatis中定义Mapper信息有两种方式,一种是利用xml写一个对应的包含Mapper信息的配置文件;另一种就是定义一个Mapper接口,然后定义一些相应的操作方法,再辅以相应的操作注解。conf.xml代码,具体参数的解释请看文档。
1 | <?xml version="1.0" encoding="UTF-8"?> |
4.新建一个实体类User。
1 | package com.MyBatis.test1; |
5.新建userMapper.xml文件,文件参数解释请看文档,代码如下:1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
"http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<mapper namespace="com.MyBatis.test1.userMapper">
<select id="getUser" parameterType="int"
resultType="com.MyBatis.test1.User">
select * from users where id=#{id}
</select>
<select id = "insertUser" parameterType="com.MyBatis.test1.User">
insert into users(name,age) values(#{name},#{age});
</select>
<delete id="deleteUser" parameterType="int">
delete from users where id=#{id};
</delete>
<update id="updateUser" parameterType="com.MyBatis.test1.User">
update users set name =#{name},age=#{age} where id = #{id};
</update>
<select id="selectAllUsers" resultType="com.MyBatis.test1.User">
select * from users;
</select>
</mapper>
6.新建 测试类test,此处采用JUnit4测试,然后逐个方法进行测试。1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49package com.MyBatis.test1;
import java.io.InputStream;
import java.util.List;
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;
import org.apache.ibatis.session.SqlSessionFactoryBuilder;
import org.junit.Test;
public class Test1 {
String resource = "conf.xml";
InputStream is = Test1.class.getClassLoader().getResourceAsStream(resource);
SqlSessionFactory factory = new SqlSessionFactoryBuilder().build(is);
SqlSession session = factory.openSession(true);
UserMapper1 mapper = session.getMapper(UserMapper1.class);
public void insert() {
User user = new User();
user.setName("dudefu");
user.setAge(20);
String statementInsert = "com.MyBatis.test1.userMapper.insertUser";
session.insert(statementInsert, user);
}
public void update() {
User user = new User();
user.setName("ddf");
user.setAge(20);
user.setId(6);
String statement = "com.MyBatis.test1.userMapper.updateUser";
session.update(statement, user);
}
public void selectAllUsers() {
String statementSelectAll = "com.MyBatis.test1.userMapper.selectAllUsers";
List<User> selectAllUser = session.selectList(statementSelectAll);
System.out.println(selectAllUser);
}
public void delete() {
String statement = "com.MyBatis.test1.userMapper.deleteUser";
int a = session.delete(statement, 10);
System.out.println(a);
}
}
7.采用注解的方式。首先新建接口类userMapper1,此处注意,如果接口类和userMpper.xml在同一个包内,则接口类的名字不能和userMapper.xml的名字相同,否则会报错java.lang.NoClassDefFoundError: com/MyBatis/test1/userMapper (wrong name: com/MyBatis/test1/UserMapper
具体原因,启动程序后,程序首先会加载所有文件,这时userMpper.class和userMpper.xml会发生冲突。所以如果在同一个包内需改名,此处改名为userMapper1.class。userMapper1.class代码如下:1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25package com.MyBatis.test1;
import java.util.List;
import org.apache.ibatis.annotations.Delete;
import org.apache.ibatis.annotations.Insert;
import org.apache.ibatis.annotations.Select;
import org.apache.ibatis.annotations.Update;
public interface UserMapper1 {
"insert into users(name,age) values(#{name},#{age})") (
public int insertUser(User user);
"delete from users where id=#{id}") (
public int deleteUserById(int id);
"update users set name =#{name},age=#{age} where id = #{id}") (
public int updateUser(User user);
"select * from users where id=#{id}") (
public User getUserById(int id);
"select * from users") (
public List<User> getAllUser();
}
8.新建测试类test2。代码如下:1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38package com.MyBatis.test2;
import java.io.InputStream;
import java.util.List;
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;
import org.apache.ibatis.session.SqlSessionFactoryBuilder;
import org.junit.Test;
public class Test2 {
String resource = "conf.xml";
InputStream is = Test1.class.getClassLoader().getResourceAsStream(resource);
SqlSessionFactory factory = new SqlSessionFactoryBuilder().build(is);
SqlSession session = factory.openSession(true);
UserMapper1 mapper = session.getMapper(UserMapper1.class);
public void testInsert(){
int insert = mapper.insertUser(new User(-1,"dudefu",23));
System.out.println(insert);
}
public void testUpdate(){
int update = mapper.updateUser(new User(11,"ddf",34));
System.out.println(update);
}
public void testdelete(){
int delete = mapper.deleteUserById(11);
System.out.println(delete);
}
public void selectAllUser(){
List<User> selectAllUsers = mapper.getAllUser();
System.out.println(selectAllUsers);
}
}
按照以上步骤进行操作,测试应该是不报错的。如果出错了,一般出错在 conf.xml中的路径配置,如下段代码;
1 | <mappers> |
和userMapper.xml中。