发布时间:2023-06-09 12:00
ORM 全称Object Relation Mapping : 表示对象-关系映射的缩写
ORM 完成面向对象的编程语言到关系数据库的映射,它把关系数据库包装成面向对象的模型。采用ORM框架后,应用程序不再直接访问底层数据库,而是以面向对象的方式来操作持久化对象,ORM将把这些对象的操作转换成底层SQL操作。
1.创建一个maven项目
并添加pom.xml文件
UTF-8
UTF-8
1.8
1.8
1.8
com.w
mybatis_persistence
1.0-SNAPSHOT
junit
junit
4.12
2.创建数据库并添加数据
create table user
(
id int null,
username varchar(20) default \'\'
);
insert into user (id,username) values (1,\'zhangsan\');
insert into user (id,username) values (2,\'lisi\');
3.创建实体类
package com.w.pojo;
public class User {
private Integer id;
private String name;
public Integer getId() {
return id;
}
public void setId(Integer id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
@Override
public String toString() {
return \"User{\" +
\"id=\" + id +
\", name=\'\" + name + \'\\\'\' +
\'}\';
}
}
4.编写UserMapper.xml
insert into user values(#{id},#{name})
update user set name=#{name} where id=#{id}
delete from user where id=#{id}
5.编写sqlMapConfig.xml
6.编写测试类
package com.w;
import com.w.dao.UserDao;
import com.w.pojo.User;
import com.w.session.SessionFactory;
import com.w.session.SessionFactoryBuilder;
import com.w.io.Resource;
import com.w.session.SqlSession;
import org.junit.Test;
import java.io.InputStream;
import java.util.List;
public class TestDemo {
/**
* getMapper方式查询delete
*/
@Test
public void testDelete() throws Exception{
SessionFactoryBuilder sessionFactoryBuilder = new SessionFactoryBuilder();
SessionFactory build = sessionFactoryBuilder.build(Resource.getResourceAsStream(\"sqlMapConfig.xml\"));
SqlSession sqlSession = build.openSesssion();
User inputUser = new User();
inputUser.setId(5);
//inputUser.setName(\"tom5\");
UserDao userDao = sqlSession.getMapper(UserDao.class);
userDao.deleteUserById(inputUser);
}
/**
* getMapper方式查询update
*/
@Test
public void testUpdate() throws Exception{
SessionFactoryBuilder sessionFactoryBuilder = new SessionFactoryBuilder();
SessionFactory build = sessionFactoryBuilder.build(Resource.getResourceAsStream(\"sqlMapConfig.xml\"));
SqlSession sqlSession = build.openSesssion();
User inputUser = new User();
inputUser.setId(5);
inputUser.setName(\"tom5\");
UserDao userDao = sqlSession.getMapper(UserDao.class);
userDao.updateUser(inputUser);
}
/**
* getMapper方式查询insert
*/
@Test
public void testInsertUserByMapper() throws Exception{
SessionFactoryBuilder sessionFactoryBuilder = new SessionFactoryBuilder();
SessionFactory build = sessionFactoryBuilder.build(Resource.getResourceAsStream(\"sqlMapConfig.xml\"));
SqlSession sqlSession = build.openSesssion();
User inputUser = new User();
inputUser.setId(5);
inputUser.setName(\"tom\");
UserDao userDao = sqlSession.getMapper(UserDao.class);
userDao.insertUser(inputUser);
}
/**
* getMapper方式查询select
*/
@Test
public void testSelectListBygetMapper() throws Exception{
SessionFactoryBuilder sessionFactoryBuilder = new SessionFactoryBuilder();
SessionFactory build = sessionFactoryBuilder.build(Resource.getResourceAsStream(\"sqlMapConfig.xml\"));
SqlSession sqlSession = build.openSesssion();
User inputUser = new User();
inputUser.setId(1);
UserDao userDao = sqlSession.getMapper(UserDao.class);
List users = userDao.findAll();
for (User user : users) {
System.out.println(user);
}
}
/**
* getMapper方式查询selectOne
*/
@Test
public void testSelectOneBygetMapper() throws Exception{
SessionFactoryBuilder sessionFactoryBuilder = new SessionFactoryBuilder();
SessionFactory build = sessionFactoryBuilder.build(Resource.getResourceAsStream(\"sqlMapConfig.xml\"));
SqlSession sqlSession = build.openSesssion();
User inputUser = new User();
inputUser.setId(1);
UserDao userDao = sqlSession.getMapper(UserDao.class);
User user = userDao.findOne(inputUser);
System.out.println(user);
}
/**
* select方法
*/
@Test
public void testSelectById() throws Exception{
SessionFactoryBuilder sessionFactoryBuilder = new SessionFactoryBuilder();
SessionFactory build = sessionFactoryBuilder.build(Resource.getResourceAsStream(\"sqlMapConfig.xml\"));
SqlSession sqlSession = build.openSesssion();
User inputUser = new User();
inputUser.setId(1);
User user = sqlSession.selectOne(\"com.w.dao.UserDao.findOne\", inputUser);
System.out.println(user);
}
/**
* 测试获取SessionFactoryBuilder
*/
@Test
public void testSessionFactoryBuilder() throws Exception{
SessionFactoryBuilder sessionFactoryBuilder = new SessionFactoryBuilder();
SessionFactory build = sessionFactoryBuilder.build(Resource.getResourceAsStream(\"sqlMapConfig.xml\"));
SqlSession sqlSession = build.openSesssion();
List users = sqlSession.selectList(\"com.w.dao.UserDao.findAll\");
for (User user : users) {
System.out.println(user);
}
}
/**
* 测试获取输入流对象是否成功
*/
@Test
public void testGetResource(){
InputStream resource = Resource.getResourceAsStream(\"sqlMapConfig.xml\");
System.out.println(resource);
}
/**
* 第一个测试类。。。
*/
@Test
public void test1(){
System.out.println(\"测试一下。。。\");
}
}
执行查询所有,即完成mybatis基本查询了。