Mybatis是一个优秀的持久层框架,它对 jdbc 的操作数据库的过程进行封装,使开发者只需要关注 SQL 本身,而不需要花费精力去处理例如注册驱动、创建 connection、创建 statement、手动设置参数、结果集检索等 jdbc 繁杂的过程代码。
地址:https://github.com/mybatis/mybatis-3/releases
1、创建Java Porject
2、导入jar包
3、配置mybatis.xml
在 src 下创建创建 mybatis 核心配置文件 mybatis.xml
4、创建sql语句
CREATE TABLE `t_message` (
`msgid` int(11) NOT NULL AUTO_INCREMENT,
`userid` int(11) NOT NULL,
`msgtopic` varchar(200) NOT NULL,
`msgcontents` varchar(1000) NOT NULL,
`msgtime` varchar(30) NOT NULL,
`msgip` varchar(30) DEFAULT NULL,
PRIMARY KEY (`msgid`),
KEY `FK_gtgh1uogevfltp7c84kpq05eu` (`userid`),
CONSTRAINT `FK_gtgh1uogevfltp7c84kpq05eu` FOREIGN KEY (`userid`) REFERENCES `t_user` (`userid`)
) ENGINE=InnoDB AUTO_INCREMENT=80 DEFAULT CHARSET=utf8;
5、根据数据库创建pojo,包括get、set、有参无参构造器、toString 方法
6、创建UserMapper.xml
insert into user(username,birthday,sex,address) values(#{username},#{birthday},#{sex},#{address})
update user set username=#{username},address=#{address} where id=#{id}
delete from user where id=#{id}
7、测试
package com.szy.test;
import java.io.IOException;
import java.io.InputStream;
import java.util.Date;
import java.util.List;
import org.apache.ibatis.io.Resources;
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;
import org.apache.ibatis.session.SqlSessionFactoryBuilder;
import org.junit.After;
import org.junit.Before;
import org.junit.Test;
import com.szy.pojo.User;
public class UserTest {
private SqlSession sqlSession =null;
//@Test之前执行
@Before
public void init() throws IOException {
System.err.println("初始化");
//1.加载mybatis的全局配置文件 io的形式读取
InputStream in = Resources.getResourceAsStream("mybatis.xml");
//2.获取回话工厂,读取全局配置文件获取
SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(in);
//3.获取会话,通过回话工厂创建
sqlSession = sqlSessionFactory.openSession();
}
@Test
public void testSelectById() throws IOException{
//4.调用会话的方法,进行查询
//statement:预处理语句 查询需要执行的SQL语句 命名空间.id
//parameter:参数
User user = sqlSession.selectOne("UserMapper.selectById", 116);
System.err.println(user);
}
//根据姓名模糊查询
@Test
public void testSelectByName(){
List users = sqlSession.selectList("UserMapper.selectByName", "三");
for (User user : users) {
System.err.println(user);
}
}
//根据姓名模糊查询
@Test
public void testSelectByName2(){
List users = sqlSession.selectList("UserMapper.selectByName2", "%三%");
for (User user : users) {
System.err.println(user);
}
}
//新增用户
@Test
public void addUser(){
User user = new User("小王", new Date(), "男", "山东");
int insert = sqlSession.insert("UserMapper.addUser",user );
sqlSession.commit();
System.err.println(insert);
System.err.println("新增的id"+user.getId());
}
//修改用户
@Test
public void updateUser() {
User user = new User();
user.setId(138);
user.setUsername("小旺旺");
user.setAddress("上海");
int update = sqlSession.update("UserMapper.updateUser", user);
sqlSession.commit();
System.err.println(update);
}
//删除用户
@Test
public void deleteUser() {
int delete = sqlSession.delete("UserMapper.deleteUser", 138);
sqlSession.commit();
System.err.println(delete);
}
//@Test之后执行
@After
public void after() {
sqlSession.close();//资源释放
System.err.println("执行完毕");
}
}