安装mybatis依赖jar包:mybatis-3.4.6.jar
连个辅助jar包:①打印日志jar包log4j②commons
1、 log4j.properties
在classpath(resources)下创建log4j.properties如下:
# Global logging configuration
#在开发环境日志级别要设置为DEBUG、生产环境要设置为INFO或者ERROR
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
db.properties
# 右键,属性--》utf-8
# 数据库配置文件 name = value的形式配置
jdbc.driverClassName=com.mysql.jdbc.Driver
jdbc.url = jdbc:mysql://localhost:3306/java2demo?useUnicode=true&characterEncoding=utf8
jdbc.user = root
jdbc.password = root
2、SqlMapConfig.xml
配置 Mybatis 的运行环境、数据源、事务等
<?xml version="1.0" encoding="UTF-8"?>
<!-- 配置文件生名(可以用来扩展用哪些标签)-->
<!DOCTYPE configuration
PUBLIC "-//mybatis.org//DTD Config 3.0//EN"
"http://mybatis.org/dtd/mybatis-3-config.dtd">
<!--<configuration> 是根节点,有它才能在它中写别的标签-->
<configuration>
<!-- 配置pojo下文件的映射路径 -->
<typeAliases>
<!-- 包的类型别名,配置完后,该包下所有类的别名都默认是自己的类名-->
<package name="com.neuedu.pojo"/>
<!-- 自定义别名-->
<!-- <typeAlias type="com.neuedu.pojo.Emp" alias="ABC"></typeAlias-->
</typeAliases>
<!--加载数据库配置文件-->
<properties resource="db.properties"></properties>
<!--加载环境,读取配置文件-->
<environments default="development">
<environment id="development">
<transactionManager type="JDBC"/>
<dataSource type="POOLED">
<property name="driver" value="${jdbc.driver}"/>
<property name="url" value="${jdbc.url}"/>
<property name="username" value="${jdbc.user}"/>
<property name="password" value="${jdbc.password}"/>
</dataSource>
</environment>
</environments>
<!-- mapper映射器(连接数据库文件所在包(dao层、持久层),
mybatis会到该包下找对应的xml文件实现映射) -->
<mappers>
<package name="com.mapper"></package>
</mappers>
</configuration>
定义Mapper接口
public interface UserMapper {
public User findUserById(int id);
}
定义UserMapper.xml
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE mapper
PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
"http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<mapper namespace="com.mapper.UserMapper">
<!-- id:方法名 parameterType传参类型 resultType返回值类型-->
<select id="findUserById" parameterType="int" resultType="com.domain.User">
select * from user where id = #{1}
</select>
</mapper>
主方法测试
public static void main(String[] args) {
// 连接数据库java2demo,对emp表中数据进行增删改查
// 如何调用EmpMapper的映射完成查询
//得到配置文件流
InputStream inputStream = null;
try {
//1.得到配置文件流
inputStream = Resources.getResourceAsStream("SqlMapConfig.xml");
//2.创建会话工厂,传入Mybatis的配置文件信息
SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(inputStream);
//3.利用工厂对象创建SqlSession对象
SqlSession sqlSession = sqlSessionFactory.openSession();
//4.代理模式,创建usermapper对象,mybatis自动生成代理对象
EmpMapper empMapper = sqlSession.getMapper(EmpMapper.class);
// 1.根据id查询员工信息
Emp emp = empMapper.serchByEmpno(7369);
System.out.println(emp.getEname()+","+emp.getEmpno()+","+emp.getHiredate()+","+emp.getSal());
} catch (IOException e) {
e.printStackTrace();
}
}
练习
package com.neuedu.mapper;
import com.neuedu.pojo.Emp;
import java.util.List;
public interface EmpMapper {
//根据empno查询某个员工信息
Emp serchByEmpno(Integer empno);
// 查询全部员工信息
List<Emp> serchAll();
// 查询员工某 部门工资xxx以上
List<Emp> serchByOptions(Emp emp);
}
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE mapper
PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
"http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<mapper namespace="com.neuedu.mapper.EmpMapper">
<!-- id:方法名 parameterType传参类型 resultType返回值类型-->
<select id="serchByEmpno" parameterType="Integer" resultType="Emp">
select * from emp where empno = #{value}
</select>
<!-- 返回值为list或set返回值写泛型的类型-->
<select id="serchAll" resultType="Emp">
select * from emp
</select>
<!-- #{deptno}实际上是获取deptno的get方法-->
<select id="serchByOptions" resultType="Emp" parameterType="Emp">
select * from emp where deptno = #{deptno} and sal > #{sal}
</select>
</mapper>
package com.neuedu.test;
import com.neuedu.mapper.EmpMapper;
import com.neuedu.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 java.io.IOException;
import java.io.InputStream;
import java.util.List;
public class MybatisTest {
public static void main(String[] args) {
// 连接数据库java2demo,对emp表中数据进行增删改查
// 如何调用EmpMapper的映射完成查询
//得到配置文件流
InputStream inputStream = null;
try {
//1.得到配置文件流
inputStream = Resources.getResourceAsStream("SqlMapConfig.xml");
//2.创建会话工厂,传入Mybatis的配置文件信息
SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(inputStream);
//3.利用工厂对象创建SqlSession对象
SqlSession sqlSession = sqlSessionFactory.openSession();
//4.代理模式,创建usermapper对象,mybatis自动生成代理对象
EmpMapper empMapper = sqlSession.getMapper(EmpMapper.class);
// 1.根据id查询员工信息
// Emp emp = empMapper.serchByEmpno(7369);
// System.out.println(emp.getEname()+","+emp.getEmpno()+","+emp.getHiredate()+","+emp.getSal());
// 2.查询所有员工信息
// List<Emp> list = empMapper.serchAll();
// for(Emp emp : list){
// System.out.println(emp.getEname()+","+emp.getEmpno()+","+emp.getHiredate()+","+emp.getSal());
// }
// 3.查询某部门,工资xxx以上的员工信息
Emp emp1 = new Emp();
emp1.setDeptno(20);
emp1.setSal(3000.0);
List<Emp> list = empMapper.serchByOptions(emp1);
for(Emp emp : list){
System.out.println(emp.getEname()+","+emp.getEmpno()+","+emp.getHiredate()+","+emp.getSal());
}
} catch (IOException e) {
e.printStackTrace();
}
}
}
网友评论