Mybatis注解开发使用Results注解实现DQL查询语句赋值对象的功能-----Mybatis框架

发布于:2023-09-22 ⋅ 阅读:(76) ⋅ 点赞:(0)
<?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>
<!--        全局的延迟加载的开关,默认为false,全局的分步查询都支持-->
        <setting name="lazyLoadingEnabled" value="true"/>
<!--        <setting name="mapUnderscoreToCamelCase" value="true"/>-->
        <setting name="logImpl" value="SLF4J"/>
        <setting name="cacheEnabled" value="true"/>
    </settings>
    <typeAliases>
        <package name="com.powernode.mybatis.pojo"/>
    </typeAliases>
<!--    mybatis分页的拦截器-->
    <plugins>
        <plugin interceptor="com.github.pagehelper.PageInterceptor"></plugin>
    </plugins>
    <environments default="development">
        <environment id="development">
            <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.mappers"/>
    </mappers>
</configuration>
<?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>
<!--        全局的延迟加载的开关,默认为false,全局的分步查询都支持-->
        <setting name="lazyLoadingEnabled" value="true"/>
<!--        <setting name="mapUnderscoreToCamelCase" value="true"/>-->
        <setting name="logImpl" value="SLF4J"/>
        <setting name="cacheEnabled" value="true"/>
    </settings>
    <typeAliases>
        <package name="com.powernode.mybatis.pojo"/>
    </typeAliases>
<!--    mybatis分页的拦截器-->
    <plugins>
        <plugin interceptor="com.github.pagehelper.PageInterceptor"></plugin>
    </plugins>
    <environments default="development">
        <environment id="development">
            <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.mappers"/>
    </mappers>
</configuration>
package com.powernode.mybatis.Test;

import com.powernode.mybatis.mappers.CarMapper;
import com.powernode.mybatis.pojo.Car;
import com.powernode.mybatis.utils.SqlSessionUtils;
import org.apache.ibatis.session.SqlSession;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

public class Test
{
    private static final Logger logger = LoggerFactory.getLogger(Test.class);
    @org.junit.Test
    public void update()
    {
        SqlSession sqlSession = SqlSessionUtils.openSession();
        CarMapper mapper = sqlSession.getMapper(CarMapper.class);
        Car car = new Car(52L,"6666","比亚迪海豚",10L,"2020-11-11","新能源");
        mapper.update(car);
        sqlSession.commit();
        SqlSessionUtils.close(sqlSession);
    }
    @org.junit.Test
    public void deleteById()
    {
        SqlSession sqlSession = SqlSessionUtils.openSession();
        CarMapper mapper = sqlSession.getMapper(CarMapper.class);
        int count = mapper.deleteById(49L);
        sqlSession.commit();
        SqlSessionUtils.close(sqlSession);
    }
    @org.junit.Test
    public void TestInsert()
    {
        SqlSession sqlSession = SqlSessionUtils.openSession();
        CarMapper mapper = sqlSession.getMapper(CarMapper.class);
        Car car = new Car(null,"6666","比亚迪海豚",10L,"2020-11-11","新能源");
        int count = mapper.insert(car);
        sqlSession.commit();
        SqlSessionUtils.close(sqlSession);
    }
    @org.junit.Test
    public void selectById()
    {
        SqlSession sqlSession = SqlSessionUtils.openSession();
        CarMapper mapper = sqlSession.getMapper(CarMapper.class);
        Car car = mapper.selectById(50L);
        //之所以输出的信息是有值的,是因为我们启用了驼峰命名,自动映射完成的
        //也可以使用result注释实现给属性映射赋值
        logger.info(car.toString());
        SqlSessionUtils.close(sqlSession);
    }
}
package com.powernode.mybatis.Test;

import com.powernode.mybatis.mappers.CarMapper;
import com.powernode.mybatis.pojo.Car;
import com.powernode.mybatis.utils.SqlSessionUtils;
import org.apache.ibatis.session.SqlSession;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

public class Test
{
    private static final Logger logger = LoggerFactory.getLogger(Test.class);
    @org.junit.Test
    public void update()
    {
        SqlSession sqlSession = SqlSessionUtils.openSession();
        CarMapper mapper = sqlSession.getMapper(CarMapper.class);
        Car car = new Car(52L,"6666","比亚迪海豚",10L,"2020-11-11","新能源");
        mapper.update(car);
        sqlSession.commit();
        SqlSessionUtils.close(sqlSession);
    }
    @org.junit.Test
    public void deleteById()
    {
        SqlSession sqlSession = SqlSessionUtils.openSession();
        CarMapper mapper = sqlSession.getMapper(CarMapper.class);
        int count = mapper.deleteById(49L);
        sqlSession.commit();
        SqlSessionUtils.close(sqlSession);
    }
    @org.junit.Test
    public void TestInsert()
    {
        SqlSession sqlSession = SqlSessionUtils.openSession();
        CarMapper mapper = sqlSession.getMapper(CarMapper.class);
        Car car = new Car(null,"6666","比亚迪海豚",10L,"2020-11-11","新能源");
        int count = mapper.insert(car);
        sqlSession.commit();
        SqlSessionUtils.close(sqlSession);
    }
    @org.junit.Test
    public void selectById()
    {
        SqlSession sqlSession = SqlSessionUtils.openSession();
        CarMapper mapper = sqlSession.getMapper(CarMapper.class);
        Car car = mapper.selectById(50L);
        //之所以输出的信息是有值的,是因为我们启用了驼峰命名,自动映射完成的
        //也可以使用result注释实现给属性映射赋值
        logger.info(car.toString());
        SqlSessionUtils.close(sqlSession);
    }
}
package com.powernode.mybatis.mappers;

import com.powernode.mybatis.pojo.Car;
import com.powernode.mybatis.pojo.CarExample;
import java.util.List;

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);
    long countByExample(CarExample example);
    @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);
    int deleteByExample(CarExample example);
    @Delete("delete from t_car where id = #{id}")
    int deleteById(Long id);
    int deleteByPrimaryKey(Long id);
    @Insert("insert into t_car values(null,#{carNum},#{brand},#{guidePrice},#{produceTime},#{carType})")
    int insert(Car row);

    int insertSelective(Car row);

    List<Car> selectByExample(CarExample example);

    Car selectByPrimaryKey(Long id);

    int updateByExampleSelective(@Param("row") Car row, @Param("example") CarExample example);

    int updateByExample(@Param("row") Car row, @Param("example") CarExample example);

    int updateByPrimaryKeySelective(Car row);

    int updateByPrimaryKey(Car row);
}
package com.powernode.mybatis.mappers;

import com.powernode.mybatis.pojo.Car;
import com.powernode.mybatis.pojo.CarExample;
import java.util.List;

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);
    long countByExample(CarExample example);
    @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);
    int deleteByExample(CarExample example);
    @Delete("delete from t_car where id = #{id}")
    int deleteById(Long id);
    int deleteByPrimaryKey(Long id);
    @Insert("insert into t_car values(null,#{carNum},#{brand},#{guidePrice},#{produceTime},#{carType})")
    int insert(Car row);

    int insertSelective(Car row);

    List<Car> selectByExample(CarExample example);

    Car selectByPrimaryKey(Long id);

    int updateByExampleSelective(@Param("row") Car row, @Param("example") CarExample example);

    int updateByExample(@Param("row") Car row, @Param("example") CarExample example);

    int updateByPrimaryKeySelective(Car row);

    int updateByPrimaryKey(Car row);
}
本文含有隐藏内容,请 开通VIP 后查看

网站公告

今日签到

点亮在社区的每一天
去签到