org.mybatis
mybatis
3.2.6
mysql
mysql-connector-java
5.1.46
runtime
junit
junit
4.12
test
log4j
log4j
1.2.17
方式一:将数据库连接信息写入另一个properties文件,然后在mybatis-config.xml中引用
方式二:直接在mybatis-config.xml中配置
jdbc.driver=com.mysql.jdbc.Driver
jdbc.url=jdbc:mysql://127.0.0.1:3306/test
jdbc.username=root
jdbc.password=123456
起别名,给某个类起个别名,如果不起别名的话,则一般我们调用、配置的时候需要写包含包名的全路径;配置别名之后,则可以直接使用类名即可调用。两种配置方式:
(1)给单个类配置别名
(2)扫描整个包
配置mapper接口与sql的映射扫描,两种方式配置:
(1)单个文件配置
(2)扫描整个包(没有尝试成功,尚未找到原因)
特别注意:
运行过程中可能遇到找不到usermapper方法的映射的报错。
如果出现:
org.apache.ibatis.binding.BindingException: Invalid bound statement (not found)
一般的原因是Mapper interface和xml文件的定义对应不上,需要检查包名,namespace,函数名称等能否对应上,需要比较细致的对比,我经常就是写错了一两个字母搞的很长时间找不到错误
按以下步骤一一执行:
a:检查xml文件所在的package名称是否和interface对应的package名称一一对应
b:检查xml文件的namespace是否和xml文件的package名称一一对应
c:检查函数名称能否对应上
d:去掉xml文件中的中文注释
e:随意在xml文件中加一个空格或者空行然后保存
一般来说到此就可以排除错误了,如果仍未解决,还有一种可能:就是xml编译的问题,查看编译后的文件是否含有UserMapper.xml文件,如果没有那么就是编译的问题了
解决方法有两种:
a、把*Mapper.xml文件放到resource文件夹下管理
b、pom配置一下编译xml文件
src/main/java
**/*.xml
package com.sohu.zzy.mybatis.mapper;
import com.sohu.zzy.domain.User;
import org.apache.ibatis.annotations.Param;
import java.util.List;
import java.util.Map;
/**
* Created by zhuzhengyi on 2019/6/11.
*/
public interface UserMapper {
User findById(Integer id);
/**
* 查询所有用户
* @return
*/
List findAll();
/**
* multiple parameters :
method1: use the parameter order number
method2: use annotation in usermapper.java
method3:
*/
List selectMulParam1(int offset,int pagenum);
List selectMulParam2(@Param(value = "offset") int offset,
@Param(value = "pagenum") int pagenum);
List selectMulParam3(Map map);
/**
* add user
* @param user
*/
public void saveUser(User user);
/**
* update user
* @param user
*/
public void updateUser(User user);
/**
* delete user by id
* @param id
*/
public void deleteById(int id);
}
insert into user(id,name,password,age,source)VALUES (#{id},#{name},#{password},#{age},#{source})
7,采用单例模式创建sqlsessionfactory,这样没次需要调用接口访问数据库的时候可直接从工厂类中获得一个session。如果不是要该方式也可以,但是会导致每次调用接口的时候都需要创建一个session对象,比较麻烦,所以建议使用此方式。
package com.sohu.zzy.mybatis.utils;
import org.apache.ibatis.io.Resources;
import org.apache.ibatis.session.SqlSessionFactory;
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactoryBuilder;
import java.io.IOException;
import java.io.InputStream;
/**
* Created by zhuzhengyi on 2019/6/11.
*/
public class MybatisUtils {
/**
* 单例模式创建sqlsessionfactory
*/
//provide globel sqlsessionfactory
private static SqlSessionFactory sqlSessionFactory;
static {
try {
//get config file stream
InputStream ins = Resources.getResourceAsStream("mybatis-config.xml");
//create sqlsessionfactory object
sqlSessionFactory = new SqlSessionFactoryBuilder().build(ins);
} catch (IOException e) {
e.printStackTrace();
}
}
public MybatisUtils() {
}
public static SqlSessionFactory getSqlSessionFactory() {
return sqlSessionFactory;
}
public static SqlSession getSqlSession() {
return sqlSessionFactory.openSession();
}
}
8,创建测试用例
package com.sohu.zzy.test;
import com.sohu.zzy.domain.User;
import com.sohu.zzy.mybatis.mapper.UserMapper;
import com.sohu.zzy.mybatis.utils.MybatisUtils;
import org.apache.ibatis.session.SqlSession;
import org.junit.Test;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
/**
* Created by zhuzhengyi on 2019/6/11.
*/
public class UserMapperTest {
/**
* 使用接口userMapper,然后调用接口的方法
*/
@Test
public void testFindById(){
//create sqlsession object access sqlsessionfactory
SqlSession sqlSession = MybatisUtils.getSqlSession();
//get interface object by sqlsession
UserMapper userMapper = sqlSession.getMapper(UserMapper.class);
//run method in proxy object
User user = userMapper.findById(1);
sqlSession.close();
System.out.println(user.getName());
}
/**
* 不使用接口userMapper,直接调用接口的方法
*/
@Test
public void testFindById2(){
//create sqlsession object access sqlsessionfactory
SqlSession sqlSession = MybatisUtils.getSqlSession();
//run method directly without interface
User user = sqlSession.selectOne("com.sohu.zzy.mybatis.mapper.UserMapper.findById",1);
sqlSession.close();
System.out.println(user.getName());
}
/**
* test findall
*/
@Test
public void findAll(){
//create sqlsession object access sqlsessionfactory
SqlSession sqlSession = MybatisUtils.getSqlSession();
//get interface object by sqlsession
UserMapper userMapper = sqlSession.getMapper(UserMapper.class);
//run method in proxy object
List list = userMapper.findAll();
sqlSession.close();
for(User user : list){
System.out.println(user);
System.out.println(user.getName());
}
}
/**
* test multiple parameters method
*/
@Test
public void testSelectMulParam1(){
//create sqlsession object access sqlsessionfactory
SqlSession sqlSession = MybatisUtils.getSqlSession();
//get interface object by sqlsession
UserMapper userMapper = sqlSession.getMapper(UserMapper.class);
//run method in proxy object
List list = userMapper.selectMulParam1(2,2);
sqlSession.close();
for(User user : list){
System.out.println(user);
System.out.println(user.getName());
}
}
@Test
public void testSelectMulParam2(){
//create sqlsession object access sqlsessionfactory
SqlSession sqlSession = MybatisUtils.getSqlSession();
//get interface object by sqlsession
UserMapper userMapper = sqlSession.getMapper(UserMapper.class);
//run method in proxy object
List list = userMapper.selectMulParam2(2,2);
sqlSession.close();
for(User user : list){
System.out.println(user);
System.out.println(user.getName());
}
}
@Test
public void testSelectMulParam3(){
//create sqlsession object access sqlsessionfactory
SqlSession sqlSession = MybatisUtils.getSqlSession();
//get interface object by sqlsession
UserMapper userMapper = sqlSession.getMapper(UserMapper.class);
Map map = new HashMap();
map.put("offset",2);
map.put("pagenum",2);
//run method in proxy object
List list = userMapper.selectMulParam3(map);
sqlSession.close();
for(User user : list){
System.out.println(user);
System.out.println(user.getName());
}
}
@Test
public void saveUser(){
//create sqlsession object access sqlsessionfactory
SqlSession sqlSession = MybatisUtils.getSqlSession();
//get interface object by sqlsession
UserMapper userMapper = sqlSession.getMapper(UserMapper.class);
User user = new User();
user.setId(5);
user.setName("xxxx");
user.setPassword("asdfadf");
user.setAge(23);
user.setSource(2);
userMapper.saveUser(user);
sqlSession.commit();
sqlSession.close();
}
@Test
public void updateUser(){
//create sqlsession object access sqlsessionfactory
SqlSession sqlSession = MybatisUtils.getSqlSession();
//get interface object by sqlsession
UserMapper userMapper = sqlSession.getMapper(UserMapper.class);
User user = new User();
user.setId(2);
user.setName("xyyy");
user.setPassword("asdfadf");
user.setAge(23);
user.setSource(2);
userMapper.updateUser(user);
sqlSession.commit();
sqlSession.close();
}
@Test
public void deleteUser(){
//create sqlsession object access sqlsessionfactory
SqlSession sqlSession = MybatisUtils.getSqlSession();
//get interface object by sqlsession
UserMapper userMapper = sqlSession.getMapper(UserMapper.class);
userMapper.deleteById(2);
sqlSession.commit();
sqlSession.close();
}
}