0%

Mybatis的注解式开发

  • mybatis 中也提供了注解式开发方式,采用注解可以减少 Sql 映射文件的配置。

    当然,使用注解式开发的话,sql 语句是写在 java 程序中的,这种方式也会给 sql 语句的维护带来成本。

    官方建议:使用注解来映射简单语句会使代码显得更加简洁,但对于稍微复杂一点的语句,Java 注解不仅力不从心,还会让你本就复杂的 SQL 语句更加混乱不堪。 因此,如果你需要做一些很复杂的操作,最好用 XML 来映射语句。

    • 原则:简单 sql 可以注解,复杂 sql 使用 xml。
    • 注意:注解写在 SqlMapper 接口中

十七、Mybatis的注解式开发

  • mybatis 中也提供了注解式开发方式,采用注解可以减少 Sql 映射文件的配置。

    当然,使用注解式开发的话,sql 语句是写在 java 程序中的,这种方式也会给 sql 语句的维护带来成本。

    官方建议:使用注解来映射简单语句会使代码显得更加简洁,但对于稍微复杂一点的语句,Java 注解不仅力不从心,还会让你本就复杂的 SQL 语句更加混乱不堪。 因此,如果你需要做一些很复杂的操作,最好用 XML 来映射语句。

    • 原则:简单 sql 可以注解,复杂 sql 使用 xml。
    • 注意:注解写在 SqlMapper 接口中

17.1 @Insert

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
package com.f.mybatis.mapper;

import com.f.mybatis.pojo.Car;
import org.apache.ibatis.annotations.Insert;

/**
* @author fzy
* @date 2024/1/13 20:02
*/
public interface CarMapper {
/**
* 使用注解的方式插入汽车信息
*
* @param car
* @return
*/
@Insert("INSERT INTO t_car VALUES (null, #{carNum}, #{brand}, #{guidePrice}, #{produceTime}, #{carType})")
int insert(Car car);
}
1
2
3
4
5
6
7
8
@Test
public void testInsert() {
SqlSession sqlSession = SqlSessionUtil.openSession();
CarMapper mapper = sqlSession.getMapper(CarMapper.class);
int count = mapper.insert(new Car(null, "1331", "宝马", 36.80, "2001-10-31", "新能源"));
sqlSession.commit();
SqlSessionUtil.close(sqlSession);
}

17.2 @Delete

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
package com.f.mybatis.mapper;

import com.f.mybatis.pojo.Car;
import org.apache.ibatis.annotations.Delete;
import org.apache.ibatis.annotations.Insert;

/**
* @author fzy
* @date 2024/1/13 20:02
*/
public interface CarMapper {
/**
* 使用注解的方式删除汽车的信息
*
* @param id
* @return
*/
@Delete("DELETE FROM t_car WHERE id = #{id}")
int deleteById(Long id);
}
1
2
3
4
5
6
7
8
@Test
public void testDeleteById() {
SqlSession sqlSession = SqlSessionUtil.openSession();
CarMapper mapper = sqlSession.getMapper(CarMapper.class);
int count = mapper.deleteById(4L);
sqlSession.commit();
SqlSessionUtil.close(sqlSession);
}

17.3 @Update

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
package com.f.mybatis.mapper;

import com.f.mybatis.pojo.Car;
import org.apache.ibatis.annotations.Delete;
import org.apache.ibatis.annotations.Insert;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Update;

/**
* @author fzy
* @date 2024/1/13 20:02
*/
public interface CarMapper {
/**
* 使用注解的方式更新汽车的信息
*
* @param id
* @return
*/
@Update("UPDATE t_car SET car_num = #{carNum} WHERE id = #{id}")
int updateById(@Param("id") Long id, @Param("carNum") String carNum);
}
1
2
3
4
5
6
7
8
@Test
public void testUpdateById() {
SqlSession sqlSession = SqlSessionUtil.openSession();
CarMapper mapper = sqlSession.getMapper(CarMapper.class);
int count = mapper.updateById(8L, "666");
sqlSession.commit();
SqlSessionUtil.close(sqlSession);
}

17.4 @Select

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
package com.f.mybatis.mapper;

import com.f.mybatis.pojo.Car;
import org.apache.ibatis.annotations.*;

/**
* @author fzy
* @date 2024/1/13 20:02
*/
public interface CarMapper {
/**
* 根据注解的方式查询汽车的信息
*
* @param id
* @return
*/
@Select("SELECT * FROM t_car WHERE id = #{id}")
Car selectById(Long id);
}
1
2
3
4
5
6
7
8
@Test
public void testSelectById() {
SqlSession sqlSession = SqlSessionUtil.openSession();
CarMapper mapper = sqlSession.getMapper(CarMapper.class);
Car car = mapper.selectById(8L);
System.out.println(car);
SqlSessionUtil.close(sqlSession);
}
---------------The End---------------