当前位置: 首页 > 图灵资讯 > 技术篇> mybatis的注解开发

mybatis的注解开发

来源:图灵教育
时间:2023-06-11 09:19:34

1.基本介绍130

mybatis还提供注解开发⽅式,采⽤注释可以减少SQL映射⽂件的配置。

当然,使⽤如果注释开发,sql语句写在java程序中,这种语句⽅风格也会给SQL语句的维护带来成本。

官⽅就是这么说的:使⽤注释简单的语句会使代码看起来更简洁,但稍微复杂一点⼀Java注释点的句子不仅仅是⼒不从⼼,也会让你已经复杂的SQL语句更加混乱。所以,如果你需要这样做,⼀一些复杂的操作,最好⽤XML映射语句。

2.使⽤复杂的SQL注释是这样的:

mybatis的注解开发_mybati注解开发

原则:可以注释简单的sql。复杂的sql使它成为复杂的sql。⽤xml。

打包⽅式:jar

依赖:mybatis,mysql驱动,junit,logback

配置⽂件:jdbc.properties、mybatis-config.xml、logback.xml

pojo:com.powernode.mybatis.pojo.Car

mapper连接⼝:com.powernode.mybatis.mapper.CarMapper

2.1增加数据130

@Insert("insert into t_car values(null,#{carNum},#{brand},#{guidePrice},#{produceTime},#{carType})")    int insert(Car car);

///插入数据  130    @Test    public void testInsert(){        SqlSession sqlSession = SqlSessionUtil.openSession();        CarMapper mapper = sqlSession.getMapper(CarMapper.class);        Car car = new Car(null,“6666”,“丰田霸道”,32.0,“2020-11-11”,"燃油车";        int count = mapper.insert(car);        System.out.println(count);        sqlSession.commit();        sqlSession.close();    }

mybatis的注解开发_sql_02

2.2删除数据131

@Delete("delete from t_car where id = #{id}")    int deleteById(Long id);

///删除数据  131    @Test    public void testDeleteById(){        SqlSession sqlSession = SqlSessionUtil.openSession();        CarMapper mapper = sqlSession.getMapper(CarMapper.class);        int count = mapper.deleteById(48L);        System.out.println(count);        sqlSession.commit();        sqlSession.close();    }

mybatis的注解开发_mybati注解开发_03

2.3更新数据132

@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);

///更新数据  132    @Test    public void testUpdate(){        SqlSession sqlSession = SqlSessionUtil.openSession();        CarMapper mapper = sqlSession.getMapper(CarMapper.class);        Car car = new Car(6L,“6666”,“丰田霸道”,32.0,“2020-11-11”,"燃油车";        int count = mapper.update(car);        System.out.println(count);        sqlSession.commit();        sqlSession.close();    }

mybatis的注解开发_xml_04

2.4查询数据133-134

@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);

///查询数据  133    @Test    public void testSelectById(){        SqlSession sqlSession = SqlSessionUtil.openSession();        CarMapper mapper = sqlSession.getMapper(CarMapper.class);        Car car = mapper.selectById(43L);        System.out.println(car);        sqlSession.close();    }

mybatis的注解开发_mybati注解开发_05

3.代码汇总main中com.powernode.mybatis.mapperCarMapper

package com.powernode.mybatis.mapper;import com.powernode.mybatis.pojo.Car;import org.apache.ibatis.annotations.*;///注释开发  130public 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);    @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);    @Insert("insert into t_car values(null,#{carNum},#{brand},#{guidePrice},#{produceTime},#{carType})")    int insert(Car car);    @Delete("delete from t_car where id = #{id}")    int deleteById(Long id);}

test中com.powernode.mybatis.testCarMapperTest

package com.powernode.mybatis.test;import com.powernode.mybatis.mapper.CarMapper;import com.powernode.mybatis.pojo.Car;import com.powernode.mybatis.utils.SqlSessionUtil;import org.apache.ibatis.session.SqlSession;import org.junit.Test;///注释开发  130public class CarMapperTest {    ///查询数据  133    @Test    public void testSelectById(){        SqlSession sqlSession = SqlSessionUtil.openSession();        CarMapper mapper = sqlSession.getMapper(CarMapper.class);        Car car = mapper.selectById(43L);        System.out.println(car);        sqlSession.close();    }    ///更新数据  132    @Test    public void testUpdate(){        SqlSession sqlSession = SqlSessionUtil.openSession();        CarMapper mapper = sqlSession.getMapper(CarMapper.class);        Car car = new Car(6L,“6666”,“丰田霸道”,32.0,“2020-11-11”,"燃油车";        int count = mapper.update(car);        System.out.println(count);        sqlSession.commit();        sqlSession.close();    }    ///删除数据  131    @Test    public void testDeleteById(){        SqlSession sqlSession = SqlSessionUtil.openSession();        CarMapper mapper = sqlSession.getMapper(CarMapper.class);        int count = mapper.deleteById(48L);        System.out.println(count);        sqlSession.commit();        sqlSession.close();    }    ///插入数据  130    @Test    public void testInsert(){        SqlSession sqlSession = SqlSessionUtil.openSession();        CarMapper mapper = sqlSession.getMapper(CarMapper.class);        Car car = new Car(null,“6666”,“丰田霸道”,32.0,“2020-11-11”,"燃油车";        int count = mapper.insert(car);        System.out.println(count);        sqlSession.commit();        sqlSession.close();    }}

mybatis-config.xml

<?xml version="1.0" encoding="UTF-8" ?><!DOCTYPE configuration        PUBLIC "-//mybatis.org//DTD Config 3.0//EN"        "http://mybatis.org/dtd/mybatis-3-config.dtd"><configuration>    <properties resource="jdbc.properties"/>    <settings>        <setting name="mapUnderscoreToCamelCase" value="true"/>    </settings>    <typeAliases>        <package name="com.powernode.mybatis.pojo"/>    </typeAliases>    <environments default="dev">        <environment id="dev">            <transactionManager type="JDBC"/>            <dataSource type="POOLED">                <property name="driver" value="${jdbc.driver}"/>                <property name="url" value="${jdbc.url}"/>                <property name="username" value="${jdbc.username}"/>                <property name="password" value="${jdbc.password}"/>            </dataSource>        </environment>    </environments>    <mappers>        <package name="com.powernode.mybatis.mapper"/>    </mappers></configuration>

pom.xml

<?xml version="1.0" encoding="UTF-8"?><project xmlns="http://maven.apache.org/POM/4.0.0"         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">    <modelVersion>4.0.0</modelVersion>    <groupId>com.powernode</groupId>    <artifactId>course26</artifactId>    <version>1.0-SNAPSHOT</version>    <packaging>jar</packaging>    <dependencies>        <dependency>            <groupId>org.mybatis</groupId>            <artifactId>mybatis</artifactId>            <version>3.5.10</version>        </dependency>        <dependency>            <groupId>mysql</groupId>            <artifactId>mysql-connector-java</artifactId>            <version>8.0.30</version>        </dependency>        <dependency>            <groupId>junit</groupId>            <artifactId>junit</artifactId>            <version>4.13.2</version>            <scope>test</scope>        </dependency>        <dependency>            <groupId>ch.qos.logback</groupId>            <artifactId>logback-classic</artifactId>            <version>1.2.11</version>        </dependency>    </dependencies>    <properties>        <!--    jdk版本用于编译代码-->        <maven.compiler.source>1.8</maven.compiler.source>        <!--    使用jdk版本的操作程序-->        <maven.compiler.target>1.8</maven.compiler.target>    </properties></project>

剩余的

pojo

utils

logback.xml

jdbc.properties

不做赘述