十七、MyBatis的注解式开发
mybatis
中也提供了注解式开发方式,采用注解可以减少Sql映射文件的配置。
当然,使用注解式开发的话,sql语句是写在java程序中的,这种方式也会给sql语句的维护带来成本。
官方是这么说的:
使用注解来映射简单语句会使代码显得更加简洁,但对于稍微复杂一点的语句,Java 注解不仅力不从心,还会让你本就复杂的 SQL 语句更加混乱不堪。 因此,如果你需要做一些很复杂的操作,最好用 XML 来映射语句。
使用注解编写复杂的SQL是这样的:
原则:简单sql可以注解。复杂sql使用xml。
模块名:mybatis-013-annotation
打包方式:jar
依赖:mybatis,mysql驱动,junit,logback
配置文件:jdbc.properties
、mybatis-config.xml
、logback.xml
pojo:com.study.mybatis.pojo.Car
mapper接口:com.study.mybatis.mapper.CarMapper
17.1 @Insert
package com.study.mybatis.mapper;import com.study.mybatis.pojo.Car;
import org.apache.ibatis.annotations.Insert;public interface CarMapper {@Insert(value="insert into t_car values(null,#{carNum},#{brand},#{guidePrice},#{produceTime},#{carType})")int insert(Car car);
}
package com.study.mybatis.test;import com.study.mybatis.mapper.CarMapper;
import com.study.mybatis.pojo.Car;
import org.apache.ibatis.io.Resources;
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;
import org.apache.ibatis.session.SqlSessionFactoryBuilder;
import org.junit.Test;public class AnnotationTest {@Testpublic void testInsert() throws Exception{SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(Resources.getResourceAsStream("mybatis-config.xml"));SqlSession sqlSession = sqlSessionFactory.openSession();CarMapper mapper = sqlSession.getMapper(CarMapper.class);Car car = new Car(null, "1112", "卡罗拉", 30.0, "2000-10-10", "燃油车");int count = mapper.insert(car);System.out.println("插入了几条记录:" + count);sqlSession.commit();sqlSession.close();}
}
17.2 @Delete
@Delete("delete from t_car where id = #{id}")
int deleteById(Long id);
@Test
public void testDelete() throws Exception{SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(Resources.getResourceAsStream("mybatis-config.xml"));SqlSession sqlSession = sqlSessionFactory.openSession();CarMapper mapper = sqlSession.getMapper(CarMapper.class);mapper.deleteById(89L);sqlSession.commit();sqlSession.close();
}
17.3 @Update
@Update("update t_car set car_num=#{carNum},brand=#{brand},guide_price=#{guidePrice},produce_time=#{produceTime},car_type=#{carType} where id=#{id}")
int update(Car car);
@Test
public void testUpdate() throws Exception{SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(Resources.getResourceAsStream("mybatis-config.xml"));SqlSession sqlSession = sqlSessionFactory.openSession();CarMapper mapper = sqlSession.getMapper(CarMapper.class);Car car = new Car(88L,"1001", "凯美瑞", 30.0,"2000-11-11", "新能源");mapper.update(car);sqlSession.commit();sqlSession.close();
}
17.4 @Select
@Select("select * from t_car where id = #{id}")
@Results({@Result(column = "id", property = "id", id = true),@Result(column = "car_num", property = "carNum"),@Result(column = "brand", property = "brand"),@Result(column = "guide_price", property = "guidePrice"),@Result(column = "produce_time", property = "produceTime"),@Result(column = "car_type", property = "carType")
})
Car selectById(Long id);
@Test
public void testSelectById() throws Exception{SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(Resources.getResourceAsStream("mybatis-config.xml"));SqlSession sqlSession = sqlSessionFactory.openSession();CarMapper carMapper = sqlSession.getMapper(CarMapper.class);Car car = carMapper.selectById(88L);System.out.println(car);
}
执行结果: