映射文件和java工程在一起,sqlmapconfig.xml在maven webapp中的resources,但编译运行却报exception cannot find 映射文件,查看编译后的文件发现即target中没有Dept.xml解决办法
只需要在pom中加入
但在sqlmapconfig中mapper即映射路径需要这样写,以斜杠分割
configuration
PUBLIC "-//mybatis.org//DTDConfig 3.0//EN"
"http://mybatis.org/dtd/mybatis-3-config.dtd">
<mappers> <mapper resource="com/lz/mybat/User.xml"/> mappers>
但在映射配置中
xml version="1.0" encoding="UTF-8" ?> mapper PUBLIC "-//mybatis.org//DTDConfig 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd"> <mapper namespace="test"> <select id="findUserById" parameterType="int" resultType="com.lzq.mybat.User"> SELECT * FROM USER WHERE id=#{id} select> mapper>resulttype中路径是。分割,之所以是不同是因为,前者是路径以/分割,后者是类型即返回值类型pojo包类名
configuration
PUBLIC "-//mybatis.org//DTDConfig 3.0//EN"
"http://mybatis.org/dtd/mybatis-3-config.dtd">
sqlmapconfig 为config.dtd
映射为 mapper.dtd
<select id="findUserByName" parameterType="java.lang.String" resultType="com.lzq.mybat.User"> SELECT * FROM user WHERE username like #{value} select>
User user = sqlSession.selectOne("test.findUserById", 1); // 参数一,映射文件中的 namespace+.+selectid,展位副值
SELECT * FROM user WHERE username LIKE '%${value}%'
${}表示拼接符用来模糊查询之类的,需加引号,但会引起sql注入
<insert id="insertUser" parameterType="com.lzq.mybat.User"> <selectKey keyProperty="id" order="AFTER" resultType="java.lang.Integer"> SELECT last_insert_id() selectKey> INSERT INTO user(id,username,birthday,sex,address) value(#{id},#{username},#{birthday},#{sex},#{address}) insert> <insert id="insertUser" parameterType="com.lzq.mybat.User"> <selectKey keyProperty="id" order="BEFORE" resultType="java.lang.Integer"> SELECT uuid() selectKey> INSERT INTO user(id,username,birthday,sex,address) value(#{id},#{username},#{birthday},#{sex},#{address}) insert>
<typeAliases> <package name="com.lzq.mybat"/> <package name="com.lzq.mapper"/> <package name="com.lzq.dao"/> typeAliases>
<mapper class="com.lzq.mapper.UserMapper"/> <package name="com.lzq.mapper"/>
<select id="findUserList" parameterType="com.lzq.dao.UserQueryVo" resultType="com.lzq.dao.UserExpand"> SELECT * FROM user WHERE user.id = #{userExpand.sex} AND user.username LIKE '%${userExpand.user}%' select>