mybatis系统核心配置文件 mybatis-config.xml
文件头
<?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 可以配置在Java 属性配置文件中
settings 修改 MyBatis 在运行时的行为方式
typeAliases 为 Java 类型命名一个别名(简称)
typeHandlers 类型处理器
objectFactory 对象工厂
plugins 插件
environments 环境
environment 环境变量
transactionManager 事务管理器
dataSource 数据源
mappers 映射器
mybatis元素节点顺序不能打乱
mybatis配置文件
<!-- 引入 database.properties 文件--> <properties resource="database.properties"/>
配置properties元素的两种方式
1.通过外部指定的方式(database.properties),实现动态配置
2.直接配置为xml,实现动态配置
如果两种方式全部使用后 优先实现动态配置
<typeAliases> <!-- <typeAlias alias="别名" type="类完整路径"/> --> <package name="类包名完整路径"/> </typeAliases>
使用<package>标签,表示扫描该包名下的所有类(除了接口和匿名内部类),如果类名上有注解,则使用注解指定的名称作为别名,如果没有则使用类名首字母小写作为别名,如com.majing.learning.mybatis.entity.User这个类如果没有设置@Alias注解,则此时会被关联到user这个别名上。(其实是不区分大小写的 推荐使用小写)
注解 @Alias
@Alias(value="User") //如不指定 默认别名为小写 dao层xml配置文件中sql标签中使用为 user public class User{ }typeAliases配置需要放置在settings之后,否则会出异常
这里是dalao总结的 很清晰
environments元素
表示配置MyBatis的多套运行环境,将SQL映射到多个不同的数据库上
子元素节点:environment,但是必须指定其中一个为默认运行环境(通过default指定)
<?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"> <!-- 通过这个配置文件完成mybatis与数据库的连接 --> <configuration> <!-- 引入 database.properties 文件--> <properties resource="database.properties"/> <settings> <!-- 配置mybatis的log实现为LOG4J --> <setting name="logImpl" value="LOG4J" /> <!-- 设置resultMap的自动映射级别为: NONE PARTIAL(默认) FULL --> <!-- <setting name="autoMappingBehavior" value="FULL" /> --> <!-- <setting name="autoMappingBehavior" value="NONE" /> --> </settings> <!--类型别名--> <typeAliases> <!-- <typeAlias alias="User" type="cn.xixi.pojo.User"/> --> <package name="cn.xixi.entity"/> </typeAliases> <!-- spring整合后 environments配置将废除 --> <environments default="development"> <environment id="development"> <!--配置事务管理,采用JDBC的事务管理 --> <transactionManager type="JDBC"></transactionManager> <!-- POOLED:mybatis自带的数据源,JNDI:基于tomcat的数据源 --> <dataSource type="POOLED"> <property name="driver" value="${driver}"/> <property name="url" value="${url}"/> <property name="username" value="${user}"/> <property name="password" value="${password}"/> </dataSource> </environment> </environments> <mappers> <!-- dao层每个接口对应一个xml文件 多个xml文件 需多个mapper标签读取 路径为完整路径 --> <!-- 使用相对路径配置 加载mapper映射文件 --> <!-- <mapper resource="cn/xixi/mapper/UserMapper.xml"/> --> <!-- 使用完整路径配置(注意:前面必须加上file:///) --> <!-- <mapper url="file:///C:\Users\cn\xixi\mapper\UserMapper.xml"/> --> <!-- 使用接口配置 (注意:xml文件和mapper接口文件名需相同 且在同一包下) --> <!-- 例: UserMapper.java 接口和 UserMapper.xml 映射文件 --> <!-- <mapper class="cn.xixi.mapper.UserMapper" /> --> <!-- 加载指定包下所有映射文件 前提包名类名同接口配置 --> <package name="cn.xixi.mapper"/> </mappers> </configuration><!-- 事务管理器 [ JDBC | MANAGED 托管 ] -->
<transactionManager type="[ JDBC | MANAGED ]" /> <!-- dataSource dataSource元素使用基本的JDBC数据源接口来配置JDBC连接对象的资源 -->
<dataSource type=" [UNPOOLED | POOLED | JNDI]" />
接口对应一个xml文件
package cn.xixi.mapper.user; import java.util.List; import java.util.Map; import cn.xixi.entity.User; import cn.xixi.vo.UserVo; public interface UserMapper { /** * 查询用户列表(参数:对象入参) */ public List<UserVo> getUserList(User user); /** * 查询用户列表(参数:Map) */ public List<User> getUserListByMap(Map<String, String> userMap); /** * 删除指定用户bulabula 使用注解传入多个参数 @Param("参数别名") * 参数别名在xml文件中使用 #{参数别名} 使用 * */ public int delUserTypeMIMIMI(@Param("MIMI_ID") int id,@Param("MIMI_Type") int type); } <?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"> <!-- namespace命名空间 接口完整路径 --> <mapper namespace="cn.xixi.mapper.user.UserMapper"> <!-- 提取重复的sql语句 --> <sql id="mySqlSelectUser"> SELECT * FROM `user` u <!-- 使用 <include refid="mySqlSelectUser"></include> --> </sql> <!-- 当数据库中的字段信息与对象的属性不一致时需要通过resultMap来映射 --> <!-- resultMap=sql标签中resultMap对应 type=类名--> <!-- 如果sql语句为单表查询 使用resultMap,result中没有写的列名也会自动匹配 --> <!-- 如果sql语句为多表查询 使用resultMap,result中没有写的列名不会自动匹配 --> <resultMap type="cn.xixi.vo.Uservo" id="userList"> <!-- property=pojo类字段名 column=数据库表中字段名 --> <result property="id" column="id"/> <result property="userName" column="userName"/> <!-- 一对一 对象 --> <!-- property:映射数据库列的实体对象的属性 javaType:完整Java类名或者别名 resultMap:引用外部resultMap --> <association property="" javaType="" ></association> <!-- 一对多 集合 --> <!-- property:映射数据库列的实体对象的属性 ofType:完整Java类名或者别名(集合所包括的类型) resultMap:引用外部resultMap --> <collection property="" ofType=""></collection> </resultMap> <!-- id=接口方法名 resultMap=resultMap标签id对应--> <!-- parameterType=参数类型,无指定别名,则为完整路径 只能传入一个参数 可传入对象以解决 --> <!-- resultType=返回值类型,没有指定别名,则为完整路径 注意的是返回值为泛型集合 要写泛型 --> <!-- #{} sql语句参数 #{POJO字段名 对应数据库中字段} --> <!-- resultMap 和 resultType 不能一起出现 --> <!-- 查询用户列表(参数:对象入参) --> <select id="getUserList" resultMap="userList" parameterType="cn.xixi.vo.Uservo"> sql语句 </select> <!-- 查询用户列表(参数:Map) --> <select id="getUserListByMap" resultType="User" parameterType="Map"> sql语句 </select> <!-- 删除指定用户bulabula parameterType参数类型一个时设置 多个不同时 不设置 --> <select id="delUserTypeMIMIMI" resultType="int"> delete from user where id = #{MIMI_ID} and type = #{MIMI_Type} </select> <select id="queryUserListByUser" parameterType="User" resultType="User"> <!-- 查询用户性别 模糊查询用户姓名 查询用户国籍ID c_id --> <include refid="mySqlSelectUser"></include> <where> <!-- where标签代替sql语句WHERE 可以解决 sql语句中 where后多加AND的问题--> <!-- 使用where标签 sql语句中AND|OR放在后面,where标签不会去掉 导致报错--> <!-- 注意 if标签test属性中 and 不能大写 --> <if test="u_sex != null and u_sex !=''"> AND u_sex = #{u_sex} </if> <if test="u_username != null and u_username !=''"> AND u_username like "%"#{u_username}"%" </if> <if test="u_cid != null"> AND u_cid = #{u_cid}; </if> </where> </select> <select id="queryUserListByUserTrim" parameterType="User" resultType="User"> <!-- 查询用户性别 模糊查询用户姓名 查询用户国籍ID c_id --> <include refid="mySqlSelectUser"></include> <!-- trim标签 prefix="trim标签内sql语句前缀" suffix="trim标签内sql语句后缀"--> <!-- trim标签 prefixOverrides="trim标签内 拼接SQL语句 前缀去除 AND|OR(只能去掉第一个)" suffixOverrides="trim标签内 拼接SQL语句 后缀去除 AND|OR(只能去掉最后一个)"--> <trim prefix="where" suffix=";" prefixOverrides="AND" suffixOverrides="AND|OR"><!-- --> <if test="u_sex != null and u_sex !=''"> AND u_sex = #{u_sex} </if> <if test="u_username != null and u_username !=''"> AND u_username like "%"#{u_username}"%" </if> <if test="u_cid != null"> AND u_cid = #{u_cid} AND </if> </trim> </select> <!-- 更新用户信息 --> <update id="updateUserInfoByUser" parameterType="User"> <!-- 使用set标签代替sql语句set set标签会自动清除后缀 ','逗号 --> update user <set> <if test="u_username != null and u_username != ''"> u_username = #{u_username}, </if> <if test="u_sex != null and u_sex != ''"> u_sex = #{u_sex}, </if> </set> where u_id = #{u_id}; </update> <select id="queryUserListByIds" resultType="User" parameterType="Integer"> <include refid="mySqlSelectUser"></include> <where> AND u_id IN <!-- <foreach collection="array/list 如果参数类型为包装类 则为数组或集合名" item="列名或别名" open="起始字符" close="结束字符" separator="分割符 不会再最后有" index="循环下标变量别名"> --> <foreach collection="array" item="id" open="(" close=")" separator=","> #{id} </foreach> </where> </select> </mapper>resultMap属性
id:resultMap的唯一标识
type:Java实体类
resultMap子元素
id 一般对应数据库中该行的主键id,设置此项可提高MyBatis性能
result 简单类型 属性
association 对象
collection 集合
使用
/** * 测试类 * */ public class Test(){ //配置文件路径 String resource = "mybatis-config.xml"; //读取mybatis配置文件 InputStream is = Resource.getResourceAsStream(resource); //获取SqlSessionFactory对象 SqlSessionFactoryBuilder ssfb = new SqlSessionFactoryBuilder(); SqlSessionFactory ssf = ssfb.build(is); // SqlSessionFactory factory = new SqlSessionFactoryBuilder().build(is); //SqlSessionFactory ssf = ssfb.build(is); //获取SqlSession对象 SqlSession session = ssf.openSession(false);//是否自动提交 默认false true为自动提交事务 }SqlSessionFactoryBuilder //获取SqlSessionFactory对象 作用于在函数体内
SqlSessionFactory //获取SqlSession对象 作用于: Application 整个项目
SqlSession //一次请求的有效期内
SqlSession方法
//第一种方式:调用selectList方法执行查询操作 userList = sqlSession.selectList("接口名完整路径到方法",参数);
//例: userList = sqlSession.selectList("cn.xixi.dao.user.UserMapper.getUserListByMap",userMap);第二种方式:调用getMapper(Mapper.class)执行dao接口方法来实现对数据库的查询操作
userList = sqlSession.getMapper(类名.class).方法名(参数);
//例: userList = sqlSession.getMapper(UserMapper.class).getUserListByMap(userMap);selectOne();//查询结果为一个时