三大框架之一 —— mybatis 的 mybatis-config.xml 文件配置
来源:互联网 发布:萧山网络问政平台临浦 编辑:程序博客网 时间:2024/06/07 08:16
1.常用标签:
标签必须按照指定顺序书写:properties(传递配置文件), settings(设置命名方式), typeAliases(设置别名), typeHandlers?, objectFactory?, objectWrapperFactory?, reflectorFactory?, plugins?, environments(环境配置), databaseIdProvider, mappers(SQL语句的配置)
2. 举例:
<?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?, settings?, typeAliases?, typeHandlers?, objectFactory?, objectWrapperFactory?, reflectorFactory?, plugins?, environments?, databaseIdProvider?, mappers? --> <properties resource="db.properties"> <!-- 声明出来了四个变量,下面的配置文件中可以直接使用 ${变量名} 获取值 --> <property name="isUserCamelCase" value="true"/> </properties> <settings> <!-- 开启数据库下划线命名方式和 java 驼峰命名方式之间的转换 --> <setting name="mapUnderscoreToCamelCase" value="${isUserCamelCase}" /> </settings> <typeAliases> <!-- 设置别名,使用别名代替原始的名字 --> <typeAlias type="com.zhiyou100.model.User" alias="_User" /> <typeAlias type="com.zhiyou100.model.Category" alias="_Category" /> <typeAlias type="com.zhiyou100.model.Topic" alias="_Topic" /> <typeAlias type="com.zhiyou100.model.Reply" alias="_Reply" /> </typeAliases> <environments default="development"> <environment id="development"> <transactionManager type="JDBC" /> <!-- 自带可用的数据库连接池,不想使用设置 UNPOOLED --> <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> <environment id="product"> <transactionManager type="JDBC" /> <dataSource type="POOLED"> </dataSource> </environment> </environments><!-- 这里的环境配置也可以直接写,不过不推荐,不方便修改和维护 <environments default="development"> <environment id="development"> <transactionManager type="JDBC" /> <dataSource type="POOLED"> <property name="driver" value="com.mysql.jdbc.Driver" /> <property name="url" value="jdbc:mysql://localhost:3306/zyonlineforum" /> <property name="username" value="root" /> <property name="password" value="123456" /> </dataSource> </environment> </environments>--> <mappers> <!-- 使用 xml 的方式配置 --> <mapper resource="com/zhiyou100/mapper/CategoryMapper.xml" /> <!-- 使用注解的方式配置 ,直接在CategoryDao 接口方法上面写查询语句,例: @Select("select * from category") List<Category> listCategory(); --> <mapper class="com.zhiyou100.dao.CategoryDao" /> <mapper resource="com/zhiyou100/mapper/UserMapper.xml" /> </mappers></configuration>
3 . ModelMapper.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"> <!-- 对应model类接口的路径 --><mapper namespace="com.zhiyou100.dao.UserDao"> <!-- 字符串拼接使用:${value} --> <!-- select * from user where username like #{value} --> <!-- id 就是对应的方法名 parameterType 传递的参数 resultType 返回值类型 ,有就写,没有不写--> <select id="listUserByName" parameterType="String" resultType="_User"> select * from user where username like '%${value}%' <!-- 模糊查询 --> </select> <!-- 多参数: 1. 封装为对象 2. 使用 HashMap 3. 使用参数索引: arg0,arg1,... param1,param2,... --> <select id="listUserByNameOrEmail" resultType="_User"> select * from user where username like #{param1} or email like #{param2} </select></mapper>
4 . ModelMapper.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.zhiyou100.dao.UserDao"> <!-- where 标签,用来拼接条件,会忽略紧跟着的一个 and --> <!-- 登录注册功能中的查询,自动判断username 或 id 是否有输入 --> <select id="listUserBy" parameterType="_User" resultType="_User"> select * from user <where> <if test="username!=null and username!=''"> and username like #{username} </if> <if test="id!=null"> and id = #{id} </if> </where> </select><!-- 查询条件有多个,是同一类型 username,可以使用foreach --> <select id="listUserSearch" resultType="_User"> select * from user <where> <foreach collection="array" item="item"> and username like #{item} </foreach> </where> </select> <!-- foreach 循环, collection:循环的集合,默认参数名字叫做 array item:集合中的每个元素 separator:for 循环拼接后的每隔字符串之间使用这个分隔符分隔 open:for 左边的语句,可以不使用 close:for 循环右边的语句,可以不使用 --> <select id="listUserIn" resultType="_User"> select * from user <where> <!-- 查询多个 id= #{item} 值对应的user --> <foreach collection="array" item="item" separator="," open="id in (" close=")"> #{item} </foreach> </where> </select> <!-- 获取插入数据的主键: 方案1:使用 selectKey 方案2:在 insert 标签增加 useGeneratedKeys="true" keyProperty="id" 两个属性 无论哪种方案,获得的主键都在 user 对象的 id 属性中 --> <insert id="saveUser" parameterType="_User" useGeneratedKeys="true" keyProperty="id"> <!-- <selectKey order="AFTER" resultType="long" keyProperty="id"> select LAST_INSERT_ID() </selectKey> --> insert into user (username, password, email) values (#{username}, #{password}, #{email}) </insert></mapper>
5 . ModelMapper.xml 文件配置‘实用版’多表连接查询(resultMap):
<?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.zhiyou100.dao.TopicDao"> <resultMap type="_Topic" id="topicResultMap1"> <!--使用 resultMap 自定义列名和属性名 的 映射关系,表里面gmt_create这一列与Topic 类里面 publishTime 属性对应上了 --> <result property="publishTime" column="gmt_create" /> </resultMap> <select id="listTopicByCategoryId" resultMap="topicResultMap1"> select * from topics where c_id = #{cId} </select> <resultMap type="_Topic" id="topicResultMap2"> <!-- 查询出来的是 topic对象下面的一个属性 user, 标签:association --> <association property="user" javaType="_User"> <id property="id" column="id" /> <result property="password" column="password" /> <result property="username" column="username" /> </association> </resultMap> <select id="listTopicByCategoryIdwithUser" resultMap="topicResultMap2"> select t.*,user.* from topics as t,user where t.c_id =#{cId} and t.u_id = user.id; </select> <resultMap type="_Topic" id="topicResultMap3"> <id property="id" column="t_id" /> <result property="title" column="t_title"/> <!-- 查询出来的是 topic下面的一个属性user 集合 , 标签:collection --> <collection property="replies" ofType="_Reply"> <id property="id" column="r_id" /> <result property="content" column="r_content" /> </collection> </resultMap> <!-- 不能写 t.* 全部查询,一个个查询,按需要--> select t.id as t_id,t.title as t_title,r.id as r_id,r.content as r_content from topics as t,reply as r where t.id = #{id} and r.t_id = t.id; </select></mapper>
阅读全文
0 0
- 三大框架之一 —— mybatis 的 mybatis-config.xml 文件配置
- mybatis的核心xml文件配置mybatis-config.xml
- Mybatis-config.xml文件的配置
- mybatis-config.xml文件配置
- mybatis-config.xml文件配置
- mybatis-config.xml配置
- mybatis-config.xml的配置详解
- mybatis-config.xml 的配置相关信息
- mybatis之mybatis-config.xml 配置详解
- mybatis之mybatis-config.xml 配置详解
- IntelliJ配置mybatis-config.xml
- Mybatis-config.xml配置详解
- Mybatis+mysql动态分页查询数据案例——Mybatis的配置文件(mybatis-config.xml)
- 在eclipse中给mybatis的config.xml以及映射xml文件配置自动提示
- mybatis的mybatis-config.xml配置文件详解
- mybatis配置XML文件
- 【SSM-MyBatis框架】SqlMapConfig.xml的配置
- 【SSM-MyBatis框架】SqlMapConfig.xml的配置
- 找出整数序列第l大的数
- 网络菜鸟到专业人中间只差一个厉害的导师
- SpringMVC-StringHttpMessageConverter简介
- HDU 6106 Classes(水~)
- 模式匹配算法以及KMP的javascript实现
- 三大框架之一 —— mybatis 的 mybatis-config.xml 文件配置
- 树莓派搭建web服务器时安装nginx出现依赖问题解决方法
- Java:I/O流基础知识详解
- 2017年9月6日
- form表单样式案例
- 计算一个字符串中有多少个单词
- http://www.cnblogs.com/Jackie-zhang/p/6071769.html
- 旅游规划-Java
- 几种二叉树