mybatis配置文件可以在测试包里面找,但是总感觉没有自己配置的好看一些,所以放一个在这里
<?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.ss.mappers.GoodsMapper"><!-- 区分多个文件id重名可写为类名 -->
<!--resultMap是java与数据库的映射 type是java类的全名id是取一个名字可以和sql
语句中的id 名一相同也可以不相同只要保证在resultMap这个标签中是唯一的就可以 -->
<!--resultMap子标签就是与sql中对应column是数据库中的名property是java
类中的名字jdbcType是java.sql.Type中的 -->
<resultMap type="Goods"id="GoodsResult">
<id column="id"jdbcType="INTEGER" property="id"/>
<result column="Name"jdbcType="VARCHAR" property="name"/>
<result column="Price"jdbcType="DECIMAL" property="price"/>
<result column="Count"jdbcType="INTEGER" property="count"/>
</resultMap>
<!--id属性是给这条sql语句起个名字好让java代码找到它,和namespace连用调用的时候用.连接
可以把id属性名可和方法名想同(注 名字唯一性)resultMap表示这条sql语句要用哪一个
resultMap的实体类与sql的对应关系通过id属性名与之相同来确定-->
<select id="getGoodsById"parameterType="Integer" resultType="Goods">
SELECT * FROM Goods WHERE id = #{id}
</select>
<select id="getGoodsByName"parameterType="String" resultMap="GoodsResult">
SELECT * FROM Goods WHERE Name like #{name}
</select>
<!-- 全查询中间类型去掉由resultMap -->
<select id="getAllGoods" resultMap="GoodsResult">
SELECT * FROM Goods
</select>
<!-- 添加 -->
<insert id="addGoods"parameterType="Goods">
insert into Goods(name,price,count)values(#{name},#{price},#{count})
</insert>
<update id="updateGoods"parameterType="Goods">
update Goods setname=#{name},price=#{price},count=#{count} where id=#{id}
</update>
</mapper>