
导入MyBatis-3.4.5.jar和mysql-connector-java-5.1.37-bin.jar
使用Spring框架在pom.xml里面添加一下内容
LOG4Jorg.mybatis.spring.boot mybatis-spring-boot-starter 1.3.2 org.mybatis mybatis-spring 1.3.1 org.mybatis mybatis 3.5.0
在日常开发过程中,排查问题时难免需要输出MyBatis真正执行的SQL语询、参数、结果等信息,我们就可以借助LOG4J的功能来实现执行信息的输出。
使用XML方式
MyBatis接口代理方式实现dao层
1.往项目导入jar包或pom.xml依赖
2.编写核心配置文件。
使用标签
jdbc.properties文件内容,这个文件就是用来读取并使用的
driver=com.mysql.jdbc.Driver url=jdbc:mysql://127.0.0.1:3306/db0809pm username=root password=2184021338
核心配置文件包含了MyBatis最核心的设置和属性信息。如数据库的连接、事务、连接池信息等。
实现代码(通过类比便可知道上面的标签具体是什么意思)
核心根标签。
: 引入数据库连接信息配置文件标签。基本上引入的是jdbc.properties : 起别名标签。也就是给实体类起一个更加简单的名字,不需要再使用com.yy.Bean.Student,可以直接使用student :配置数据库环境标签。即数据库的相关配置
:配置数据库信息标签。
:事务管理标签。 : 数据源标签。
:数据库连接信息标签。
:引入映射配置文件标签。也就是引入所编写的映射配置文件的地址
3.编写映射配置文件。
映射配置文件包含了数据和对象之间的映射关系以及要执行的SQL语句。
: 核心根标签。即要将mapper里面的sql语句映射到对应的哪一个接口中,类似于之前使用dao和daoImpl时,dao里面的接口 namespace属性:名称空间;即接口的位置com.yy.mapper.StudentMapper
:查询功能标签。 :新增功能标签。 :修改功能标签。 :删除功能标签。 id属性:唯一标识,配合名称空间使用,最好与StudentMapper接口里面的方法名一样
parameterType属性:指定参数映射的对象类型;也就是传递的参数,如在parameterType里面传递过滤一个student学生对象,如何从学生对象里面获取id、name、age等参数
resultType属性:指定结果映射的对象类型;将查询所得的内容进行封装的位置,在这里写的是com.yy.Bean.Student,意思便是将数据封装到这个实体类中,如果返回的是int数值,所以resultType可以不用写。
- SQL获取参数
#{属性名} N
使用相应的API来完成编码。SELECT * FROM student INSERT INTO student VALUES (#{id},#{name},#{age},#{gender},#{score}) UPDATE student SET name=#{name},age=#{age},gender=#{gender},score=#{score} WHERe id = #{id} DELETE FROM student WHERe id = #{id}
在这里我只是拿出两个例子
//查询全部
@Test
public void selectAll() throws Exception{
//1.加载核心配置文件
InputStream is = Resources.getResourceAsStream("MyBatisConfig.xml");
//2.获取Sq1SessionI厂对象
SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(is);
//3.通过SqISession. I厂对象获取Sq1Session对象
SqlSession sqlSession = sqlSessionFactory.openSession();
//4.执行映射配置文件中的sq1语句,并接收结果
List list = sqlSession.selectList("StudentMapper.selectAll");
//5.处理结果
for(Student stu : list){
System.out.println(stu);
}
//6.释放资源
sqlSession.close();
is.close();
}
// 根据id查询
@Test
public void selectById() throws Exception{
//加载核心配置文件
InputStream is = Resources.getResourceAsStream("MyBatisConfig.xml");
//获取sqlSession工厂对象
SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(is);
//通过工厂对象获取SqlSession对象
SqlSession sqlSession = sqlSessionFactory.openSession();
//执行映射配置文件的sql语句,并接受结果
Student stu = sqlSession.selectOne("StudentMapper.selectById",3);
//处理结果
System.out.println(stu);
sqlSession.close();
is.close();
}
//查询全部
@Test
public void selectCondition() throws Exception{
InputStream is = Resources.getResourceAsStream("MyBatisConfig.xml");
SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(is);
SqlSession sqlSession = sqlSessionFactory.openSession(true);
StudentMapper mapper = sqlSession.getMapper(StudentMapper.class);
Student stu = new Student();
stu.setId(3);
stu.setAge(26);
// stu.setName("赵六");
List list = mapper.selectCondition(stu);
for (Student student : list){
System.out.println(student);
}
sqlSession.close();
is.close();
}
@Test
public void selectByIds() throws Exception{
InputStream is = Resources.getResourceAsStream("MyBatisConfig.xml");
SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(is);
SqlSession sqlSession = sqlSessionFactory.openSession(true);
StudentMapper mapper = sqlSession.getMapper(StudentMapper.class);
List ids = new ArrayList<>();
ids.add(1);
ids.add(2);
ids.add(3);
List list = mapper.selectByIds(ids);
for (Student student : list){
System.out.println(student);
}
sqlSession.close();
is.close();
}