- 前提条件
- 创建maybatis全局配置文件
- 编写SQL映射配置文件(mapper.xml文件)
- mapper.xml文件
- 加载映射文件
- 导入日志文件
- 编写测试代码
参考:唐浩荣–Mybatis3详解(二)----Mybatis的第一个入门实例
前提条件- 一个数据库
- 一个maven项目
- maven项目中配好依赖
- 创建一个实体类,实体类中有若干属性
- 然后进入mybatis部分
在resources目录下创建Mybatis全局配置文件mybatis-config.xml文件。
编写SQL映射配置文件(mapper.xml文件)
仍旧是在resources下创建一个mapper.xml文件,mybatis中所有数据库的 *** 作都会基于该映射文件配置的SQL语句。
mapper.xml文件中的属性的作用
加载映射文件
创建的mapper.xml文件添加到全局配置文件下
上面的已经加过了
指定映射配置文件mapper.xml的位置导入日志文件
在resources目录中创建log4j.properties文件,并且导入如下配置
# Global logging configuration log4j.rootLogger=DEBUG, stdout # Console output... log4j.appender.stdout=org.apache.log4j.ConsoleAppender log4j.appender.stdout.layout=org.apache.log4j.PatternLayout log4j.appender.stdout.layout.ConversionPattern=%5p [%t] - %m%n编写测试代码
最后创建一个测试类
package cn.itpc.mybaitis; import cn.itpc.pojo.Emp; 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.Before; import org.junit.jupiter.api.Test; import java.io.IOException; import java.io.InputStream; import java.util.HashMap; import java.util.List; import java.util.Map; //练习 查询emp表中所有的员工,返回一个List集合 public class TestMyBatis01 { // SqlSession session; // // @Before // public void init() throws IOException { // InputStream in = Resources.getResourceAsStream("mybatis-config.xml"); // SqlSessionFactory fac = new SqlSessionFactoryBuilder().build(in); // session = fac.openSession(); // // } //1、读取mybatis的核心配置文件(mybatis-config.xml) //2、通过配置信息获取一个SqlSessionFactory工厂对象 //3、通过工厂获取一个SqlSession对象 //4、通过namespace+id找到要执行的sql语句并执行 //5、输出结果 @Test public void findAll() throws IOException { InputStream in = Resources.getResourceAsStream("mybatis-config.xml"); SqlSessionFactory fac = new SqlSessionFactoryBuilder().build(in); SqlSession session = fac.openSession(); List list = session.selectList("EmpMapper.findAll"); for (Emp e:list){ System.out.println(e); } } @Test public void testInsert() throws IOException { //执行sql语句,返回执行结果 InputStream in = Resources.getResourceAsStream("mybatis-config.xml"); SqlSessionFactory fac = new SqlSessionFactoryBuilder().build(in); SqlSession session = fac.openSession(); int row=session.update("EmpMapper.insert"); //提交事务 session.commit(); System.out.println("影响的行数:"+row); } @Test public void testupdate() throws IOException { InputStream in = Resources.getResourceAsStream("mybatis-config.xml"); SqlSessionFactory fac = new SqlSessionFactoryBuilder().build(in); SqlSession session = fac.openSession(); int row=session.update("EmpMapper.update"); session.commit(); } @Test public void testdelete() throws IOException { InputStream in = Resources.getResourceAsStream("mybatis-config.xml"); SqlSessionFactory fac = new SqlSessionFactoryBuilder().build(in); SqlSession session = fac.openSession(); int row=session.delete("EmpMapper.delete"); session.commit(); } @Test public void testFindById() throws IOException { InputStream in = Resources.getResourceAsStream("mybatis-config.xml"); SqlSessionFactory fac = new SqlSessionFactoryBuilder().build(in); SqlSession session = fac.openSession(); Emp emp=session.selectOne("EmpMapper.findById"); System.out.println(emp); } @Test public void testInsert2() throws IOException { Emp emp=new Emp(); emp.setName("张飞"); emp.setJob("工程师"); emp.setSalary(12334.0); InputStream in = Resources.getResourceAsStream("mybatis-config.xml"); SqlSessionFactory fac = new SqlSessionFactoryBuilder().build(in); SqlSession session = fac.openSession(); int row = session.update("EmpMapper.insert2",emp); session.commit(); System.out.println("影响的行数:"+row); } @Test public void testupdate2() throws IOException { Emp emp=new Emp(); emp.setName("张飞"); emp.setJob("架构师"); emp.setSalary(2222.2); InputStream in = Resources.getResourceAsStream("mybatis-config.xml"); SqlSessionFactory fac = new SqlSessionFactoryBuilder().build(in); SqlSession session = fac.openSession(); int row=session.update("EmpMapper.update2",emp); session.commit(); System.out.println("影响的行数"+row); } @Test public void testInsert3() throws IOException { Map map=new HashMap<>(); map.put("name","曹 *** "); map.put("job","总裁办公室"); map.put("salary","20000"); InputStream in = Resources.getResourceAsStream("mybatis-config.xml"); SqlSessionFactory fac = new SqlSessionFactoryBuilder().build(in); SqlSession session = fac.openSession(); int row=session.update("EmpMapper.insert2",map); session.commit(); System.out.println("影响行数"+row); } @Test public void testDelete2() throws IOException { InputStream in = Resources.getResourceAsStream("mybatis-config.xml"); SqlSessionFactory fac = new SqlSessionFactoryBuilder().build(in); SqlSession session = fac.openSession(); int row=session.update("EmpMapper.delete2",5); session.commit(); System.out.println("影响行数"+row); } @Test public void testFindAll2() throws IOException { InputStream in = Resources.getResourceAsStream("mybatis-config.xml"); SqlSessionFactory fac = new SqlSessionFactoryBuilder().build(in); SqlSession session = fac.openSession(); Map map=new HashMap(); map.put("cols","id,name"); // map.put("cols","id,name,jpb,salary"); List list=session.selectList("EmpMapper.findAll2",map); for (Emp e:list){ System.out.println(e); } } @Test public void testFindAll3() throws IOException { Map map=new HashMap(); map.put("name","云"); InputStream in = Resources.getResourceAsStream("mybatis-config.xml"); SqlSessionFactory fac = new SqlSessionFactoryBuilder().build(in); SqlSession session = fac.openSession(); List list=session.selectList("EmpMapper.findAll3",map); for (Emp e:list){ System.out.println(e); } } @Test public void testFindAll4() throws IOException { Map map=new HashMap(); map.put("name","%生%"); InputStream in = Resources.getResourceAsStream("mybatis-config.xml"); SqlSessionFactory fac = new SqlSessionFactoryBuilder().build(in); SqlSession session = fac.openSession(); List list=session.selectList("EmpMapper.findAll4",map); for(Emp e:list){ System.out.println(e); } } }
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)