<?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>
<!-- 加载属性文件 -->
<properties resource="db.properties">
<!-- properties中还可以配置一些属性名和属性值 -->
<!-- properties name="jdbc.driver" value="" -->
</properties>
<!-- 全局配置参数 -->
<!-- <settring></settring> -->
<!-- 别名定义 -->
<typeAliases>
<!-- 针对单个别名定义
type:类型的路径
alias:别名
-->
<!-- <typeAlias type="com.itvast.mybatis.po.Employee" alias="employee"></typeAlias> -->
<!-- 批量别名定义 (常用)
指定包名,mybatis自动扫描包中的po类,自动定义别名,别名就是类名(首字母大写或小写都可以)
-->
<package name="com.itvast.mybatis.po"/>
</typeAliases>
<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.username}"/>
<property name="password" value="${jdbc.password}"/>
</dataSource>
</environment>
</environments>
<mappers>
<!-- 同股票resource加载单个的映射文件 -->
<mapper resource="sqlmap/User.xml"/>
<!-- <mapper resource="mapper/UserMapper.xml"/> -->
<!-- 通过mapper接口加载映射文件
遵循一些规范:需要将mapper接口的类名和mapper.xml映射文件名称保持一致,且在一个目录
上边规范的前提是:使用的是mapper代理方法
-->
<!-- <mapper class="com.itcast.mybatis.mapper.EmployeeMapper"/> -->
<!-- 批量加载mapper
指定mapper接口的报名,mybatis自动扫描包下边所有mapper接口进行加载
遵循一些规范:需要将mapper接口的类名和mapper.xml映射文件名称保持一致,且在一个目录
上边规范的前提是:使用的是mapper代理方法
-->
<package name="com.itcast.mybatis.mapper"></package>
</mappers>
</configuration>
EmployeeMapper.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">
<!-- namesoace命名空间,作用就是对sql进行分类管理,理解sql隔离
注意:使用mapper代理方法开发,namespace有特殊重要的作用
-->
<mapper namespace="com.itcast.mybatis.mapper.EmployeeMapper">
<!-- 用户信息综合查询
#{employeeCustom.salary}:取出pojo包装对象中性别的值
${employeeCustom.name}:取出pojo包装对象中用户名称
-->
<select id="findEmployeeList" parameterType="com.itvast.mybatis.po.EmployeeQueryVo" resultType="com.itvast.mybatis.po.EmployeeCustom">
select * from Employee where salary=#{employeeCustom.salary} and name like'%${employeeCustom.name}%'
</select>
<!-- 在映射文件中配置很多sql语句 -->
<!-- 通过id查询用户表的记录 -->
<!-- 通过select执行数据库查询
id:表示映射文件中的sql,成为statement的id
将sql语句封装到(mappedStatement对象中,所以将id成为statement的id
#{}表示一个占位符
parameterType:指定输入参数的类型,这里指定int型
#{id}:其中的id表示接入输入的参数,参数名称就是id,如果输入参数是简单类型,#{}中的参数名可以任意,可以value或其他名称
resultType:指定sql输出结果的所映射的java对象类型,select指定resultType表示将单条记录映射成的java对象
-->
<select id="findUserById" parameterType="int" resultType="employee">
select * from Employee where id=#{id}
</select>
<!-- 根据用户名称模糊查询用户信息
resultType:注定就是单条记录所映射的java对象类型
${}:表示清洁sql串,将接受到参数的内容不加任何修饰凭借在sql中
使用${}清洁sql,引起sql注入
${value}:接受输入参数的内容,如果传入类型是简单类型,${}中只能使用value
-->
<select id="findUserByName" parameterType="java.lang.String" resultType="employee">
select * from Employee where name like '%${value}%'
</select>
<!-- 添加用户
parameterType:指定输入参数类型是pojo(包括用户信息)
#{}中指定pojo的属性名,接受到pojo对象的属性值,mybatis通过OGNL获取对象的属性值
-->
<!-- useGeneratedKeys="true" keyProperty="id" 获得自增长的id返回到javaBean里的"id"属性里 -->
<insert id="insertUser" parameterType="com.itvast.mybatis.po.Employee" useGeneratedKeys="true" keyProperty="id">
insert into Employee(email,hiredate,name,salary,pwd,grade,department_id) values(#{email},#{hiredate},#{name},#{salary},#{pwd},#{grade},#{department_id})
</insert>
<!-- 删除用户
根据id删除影虎,需要输入id值
-->
<delete id="delectEmployee" parameterType="java.lang.Integer">
delete from Employee where id =#{id}
</delete>
<!-- 根据id更新用户
分析:
需要传入用户的id
需要传入用户的更新信息
parameterType指定Employee对象,包括id和更新信息,注意:id必须存在
#{id}:从输入Employee对象中获取id属性值
-->
<update id="updateEmployee" parameterType="com.itvast.mybatis.po.Employee">
update Employee set department_id=#{department_id},email=#{email},grade=#{grade},hiredate=#{hiredate},name=#{name},pwd=#{pwd},salary=#{salary} where id =#{id}
</update>
</mapper>
EmployeeMapper.java
package com.itcast.mybatis.mapper;
import java.util.List;
import com.itvast.mybatis.po.Employee;
import com.itvast.mybatis.po.EmployeeCustom;
import com.itvast.mybatis.po.EmployeeQueryVo;
public interface EmployeeMapper {
//根据id查询用户信息
public Employee findUserById(int id) throws Exception;
//用户信息综合查询
public List<EmployeeCustom> findEmployeeList(EmployeeQueryVo employeeQueryVo)throws Exception;
// //根据id查询用户信息
// public Employee findUserById(int id) throws Exception;
// //添加用户信息
// public void insertEmployee(Employee employee) throws Exception;
// //删除用户信息
// public void delectEmployee(int id)throws Exception;
//根据用户名称查询用户列表
public List<Employee> findUserByName(String name) throws Exception;
}
EmployeeMapperTest.java
package com.itcast.mybatis.mapper;
import java.io.IOException;
import java.io.InputStream;
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 com.itvast.mybatis.po.Employee;
import com.itvast.mybatis.po.EmployeeCustom;
import com.itvast.mybatis.po.EmployeeQueryVo;
public class EmployeeMapperTest{
public static void main(String []args) throws Exception{
InputStream inputStream=Resources.getResourceAsStream("SqlMapConfig.xml");
SqlSessionFactory sqlSessionFactory=new SqlSessionFactoryBuilder().build(inputStream);
SqlSession sqlSession=sqlSessionFactory.openSession();
// new EmployeeMapperTest().testfindUserById(sqlSessionFactory);
EmployeeMapper employeeMapper=sqlSession.getMapper(EmployeeMapper.class);
EmployeeQueryVo employeeQueryVo=new EmployeeQueryVo();
EmployeeCustom employeeCustom=new EmployeeCustom();
employeeCustom.setName("韩");
employeeCustom.setSalary(100f);
employeeQueryVo.setEmployeeCustom(employeeCustom);
List<EmployeeCustom> list=employeeMapper.findEmployeeList(employeeQueryVo);
for(EmployeeCustom a:list){
System.out.println(a.getName());
}
sqlSession.close();
}
public void testfindUserById(SqlSessionFactory sqlSessionFactory) throws Exception {
// TODO Auto-generated method stub
SqlSession sqlSession=sqlSessionFactory.openSession();
//创建UserMapper对象
EmployeeMapper employeeMapper=sqlSession.getMapper(EmployeeMapper.class);
//sqlSession.selectOne(arg0, arg1)
//调用userMapper的方法
Employee employee=employeeMapper.findUserById(1);
System.out.println(employee.toString());
sqlSession.close();
}
}