目录
一:MyBatis的注解式开发
官⽅是这么说的:
使⽤注解编写复杂的SQL是这样的:
原则:简单sql可以注解,复杂sql使⽤xml!使用注解式开发以后三兄弟之一的SqlMapper.xml文件就不需要了!
1. @Insert注解
二兄弟之一CarMapper接口,用来编写方法
package com.bjpowernode.mybatis.mapper;
import com.bjpowernode.mybatis.pojo.Car;
import org.apache.ibatis.annotations.Insert;
public interface CarMapper {
// 使用注解式开发,插入数据
@Insert("insert into t_car values(null,#{carNum},#{brand},#{guidePrice},#{produceTime},#{carType})")
int insert(Car car);
}
二兄弟之二CarMapperTest,用来测试
package com.bjpowernode.mybatis.test;
import com.bjpowernode.mybatis.mapper.CarMapper;
import com.bjpowernode.mybatis.pojo.Car;
import com.bjpowernode.mybatis.utils.SqlSessionUtil;
import org.apache.ibatis.session.SqlSession;
import org.junit.Test;
public class CarMapperTest {
@Test
public void testInsert(){
SqlSession sqlSession = SqlSessionUtil.openSession();
CarMapper mapper = sqlSession.getMapper(CarMapper.class);
// 创建Car对象
Car car = new Car(null, "666", "丰田霸道", 32.0, "2023-1-9", "燃油车");
int count = mapper.insert(car);
System.out.println(count);
sqlSession.commit();
sqlSession.close();
}
}
执行结果:
2. @Delete注解
二兄弟之一CarMapper接口,用来编写方法
package com.bjpowernode.mybatis.mapper;
import com.bjpowernode.mybatis.pojo.Car;
import org.apache.ibatis.annotations.Insert;
public interface CarMapper {
// 使用注解式开发,删除数据
@Delete("delete from t_car where id = #{id}")
int deleteById(Long id);
}
二兄弟之二CarMapperTest,用来测试
package com.bjpowernode.mybatis.test;
import com.bjpowernode.mybatis.mapper.CarMapper;
import com.bjpowernode.mybatis.pojo.Car;
import com.bjpowernode.mybatis.utils.SqlSessionUtil;
import org.apache.ibatis.session.SqlSession;
import org.junit.Test;
public class CarMapperTest {
@Test
public void testDeleteById(){
SqlSession sqlSession = SqlSessionUtil.openSession();
CarMapper mapper = sqlSession.getMapper(CarMapper.class);
int count = mapper.deleteById(40L);
System.out.println(count);
sqlSession.commit();
sqlSession.close();
}
}
执行结果:
3. @Update注解
二兄弟之一CarMapper接口,用来编写方法
package com.bjpowernode.mybatis.mapper;
import com.bjpowernode.mybatis.pojo.Car;
import org.apache.ibatis.annotations.Insert;
public interface CarMapper {
// 使用注解式开发,更新数据
@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);
}
二兄弟之二CarMapperTest,用来测试
package com.bjpowernode.mybatis.test;
import com.bjpowernode.mybatis.mapper.CarMapper;
import com.bjpowernode.mybatis.pojo.Car;
import com.bjpowernode.mybatis.utils.SqlSessionUtil;
import org.apache.ibatis.session.SqlSession;
import org.junit.Test;
public class CarMapperTest {
@Test
public void testUpdate(){
SqlSession sqlSession = SqlSessionUtil.openSession();
CarMapper mapper = sqlSession.getMapper(CarMapper.class);
// 创建Car对象,根据id进行更新
Car car = new Car(34L, "666", "丰田霸道", 32.0, "2023-1-9", "燃油车");
int count = mapper.update(car);
System.out.println(count);
sqlSession.commit();
sqlSession.close();
}
}
执行结果:
4. @Select注解
二兄弟之一CarMapper接口,用来编写方法
package com.bjpowernode.mybatis.mapper;
import com.bjpowernode.mybatis.pojo.Car;
import org.apache.ibatis.annotations.Insert;
public interface CarMapper {
// 使用注解式开发,查询数据
@Select("select * from t_car where id = #{id}")
Car selectById(Long id);
}
二兄弟之二CarMapperTest,用来测试
package com.bjpowernode.mybatis.test;
import com.bjpowernode.mybatis.mapper.CarMapper;
import com.bjpowernode.mybatis.pojo.Car;
import com.bjpowernode.mybatis.utils.SqlSessionUtil;
import org.apache.ibatis.session.SqlSession;
import org.junit.Test;
public class CarMapperTest {
@Test
public void testSelectById(){
SqlSession sqlSession = SqlSessionUtil.openSession();
CarMapper mapper = sqlSession.getMapper(CarMapper.class);
Car car = mapper.selectById(41L);
System.out.println(car);
sqlSession.close();
}
}
执行结果:
5. @Results注解
<!--启⽤驼峰命名⾃动映射-->
<settings>
<setting name="mapUnderscoreToCamelCase" value="true"/>
</settings>
package com.bjpowernode.mybatis.mapper;
import com.bjpowernode.mybatis.pojo.Car;
import org.apache.ibatis.annotations.*;
public interface CarMapper {
// 使用注解式开发,查询数据
@Select("select * from t_car where id = #{id}")
@Results({
@Result(property = "id",column = "id"),
@Result(property = "carNum",column = "car_num"),
@Result(property = "brand",column = "brand"),
@Result(property = "guidePrice",column = "guide_price"),
@Result(property = "produceTime",column = "produce_time"),
@Result(property = "carType",column = "car_type"),
})
Car selectById(Long id);
}
结语:直到今天MyBatis的学习就完美撒花了,接下来就开始Spring的学习,敬请期待!