mybatis接口用法
1、注意要点
1 namespace与接口全限定名一致
2 id和抽象函数保持一致
3 参数类型与返回类型保持一致
4 java类名与xml文件名保存一致
举例
public interface GoodsMapper {
Goods selectGoodsById();
int insertGoods(Goods goods);
}
<mapper namespace="com.study.mapper.GoodsMapper"><!--命名空间-->
<select id="selectGoodsById" resultType="com.study.entity.Goods">
select * from goods where gid=4
</select>
<insert id="insertGoods" parameterType="com.study.entity.Goods">
INSERT INTO Goods(gname,gprice,num,description,good_type,create_time)
VALUES (#{gname},#{gprice},#{num},#{description},#{goodtype},now())
</insert>
</mapper>
public class GoodsMapperTest {
SqlSessionFactory factory;
SqlSession sqlSession;
@Before//在方法前执行
public void setUp() {
try {
InputStream is= Resources.getResourceAsStream("mybatis-config.xml");//获取主配置文件
factory=new SqlSessionFactoryBuilder().build(is);//创建工厂对象
sqlSession = factory.openSession(true);//获取SqlSession(相当于Connection)
} catch (IOException e) {
e.printStackTrace();
}
}
@After//在方法执行后执行
public void tearDown() {
sqlSession.close();
}
@Test
public void selectGoodsById() throws Exception {
GoodsMapper mapper = sqlSession.getMapper(GoodsMapper.class);
Goods goods = mapper.selectGoodsById();
System.out.println(goods);
}
@Test
public void insertGoods() throws Exception {
GoodsMapper mapper = sqlSession.getMapper(GoodsMapper.class);
Goods goods = new Goods("lenovo", new BigDecimal(5000), "lianxiang", 200, 1);
mapper.insertGoods(goods);
}
}
mybatis-config.xml配置
创建一个db.properties文件,内容如下:
jdbc.driverClass=com.mysql.jdbc.Driver
jdbc.url=jdbc:mysql:///taobao?useUnicode=true&characterEncoding=utf-8
jdbc.user=root
jdbc.password=123
<!--加载配置文件-->
<properties resource="db.properties"/>
配置log4j日志,内容如下:
# Global logging configuration
log4j.rootLogger=ERROR, stdout
# MyBatis logging configuration...
log4j.logger.com.study.mapper=TRACE
# Console output...
log4j.appender.stdout=org.apache.log4j.ConsoleAppender
log4j.appender.stdout.layout=org.apache.log4j.PatternLayout
log4j.appender.stdout.layout.ConversionPattern=%5p [%t] - %m%n
<settings>
<!-- 日志的实现类,可以不写,会自动匹配 -->
<setting name="logImpl" value="LOG4J"/>
<!-- 将数据库字段的下划线自动转为驼峰命名 -->
<setting name="mapUnderscoreToCamelCase" value="true"/>
<!-- 自动映射,FULL表示无论是否关联都进行自动映射 -->
<setting name="autoMappingBehavior" value="FULL"/>
</settings>
别名
<!-- 指定包下面的所有实体类都会将类名作为别名 -->
<typeAliases>
<package name="com.study.entity"/>
</typeAliases>
<!-- 使用的使用直接使用别名Goods,不用写全限定名 -->
<select id="selectGoodsById" resultType="Goods">
select * from goods where gid=4
</select>
输出映射
resultType
当数据库字段与属性名一致时使用
resultMap
1、解决数据库字段与属性名不一致
2、关联(可以使用别名来解决)
注意:resultType与resultMap不能同时使用!
关联
关联结果 resultMap与resultMap嵌套
public class Goods {
private GoodType goodsType;//属性是pojo 普通的java对象
}
<select id="selectGoodsById" resultMap="goodsResultMap">
SELECT * FROM goods g INNER JOIN data_dictionary d ON g.good_type=d.value_id WHERE d.type_code='good_type' AND gid=4
</select>
<resultMap id="goodsResultMap" type="Goods">
<id property="gid" column="gid"/>
<!--关联结果-->
<association property="goodsType" resultMap="goodTypeResultMap"/>
</resultMap>
<resultMap id="goodTypeResultMap" type="GoodType"/>
注意:resultMap的id和association的resultMap应该保持一致,resultMap中的type="GoodType",GoodType为创建的类
关联查询 resultMap与select嵌套
<resultMap id="goodsResultMap" type="Goods">
<id property="gid" column="gid"/>
<!--关联结果-->
<association property="goodsType" select="selectGoodType" column="good_type" javaType="GoodType"/>
</resultMap>
<select id="selectGoodsById" resultMap="goodsResultMap">
SELECT * FROM goods WHERE gid=6
</select>
<select id="selectGoods" resultMap="goodsResultMap">
SELECT * FROM goods
</select>
<select id="selectGoodsType" resultType="GoodType">
SELECT * FROM data_dictionary WHERE type_code="good_type" AND value_id=#{value}
</select>
@Test
public void selectGoodsById() throws Exception {
GoodsMapper mapper = sqlSession.getMapper(GoodsMapper.class);
Goods goods = mapper.selectGoodsById();
System.out.println(goods);
}
@Test
public void selectGoods() throws Exception {
GoodsMapper mapper = sqlSession.getMapper(GoodsMapper.class);
GoodType goodType = mapper.selectGoodsType(3);
System.out.println(goodType);
List<Goods> list = mapper.selectGoods();
for (Goods goods:list) {
System.out.println(goods);
}
}
public interface GoodsMapper {
Goods selectGoodsById();
int insertGoods(Goods goods);
List<Goods> selectGoods();
GoodType selectGoodsType(int value);
}
注意
Mapped Statements collection does not contain value for com.study.mapper.GoodsMapper.selectGoodType ,遇到类似的错误提示就去xml文件中检查id是否一致(建议最好复制粘贴)