mybatis 使用自定义sql 语句(非Mapper.xml SQL)

新建一个接口 SqlBaseMapper 封装常用的增删改查


public interface SqlBaseMapper {

    /**
     * 查询单条数据返回Map<String, Object>
     *
     * @param sql sql语句
     * @return Map<String, Object>
     */
    Map<String, Object> sqlSelectOne(String sql);

    /**
     * 查询单条数据返回Map<String, Object>
     *
     * @param sql   sql语句
     * @param value 参数
     * @return Map<String, Object>
     */
    Map<String, Object> sqlSelectOne(String sql, Object value);

    /**
     * 查询单条数据返回实体类型
     *
     * @param sql        sql语句
     * @param resultType 具体类型
     * @return 定义的实体类型
     */    <T> T sqlSelectOne(String sql, Class<T> resultType);

    /**
     * 查询单条数据返回实体类型
     *
     * @param sql        sql语句
     * @param value      参数
     * @param resultType 具体类型
     * @return 定义的实体类型
     */    <T> T sqlSelectOne(String sql, Object value, Class<T> resultType);

    /**
     * 查询数据返回
     *
     * @param sql sql语句
     * @return List<Map < String, Object>>
     */
    List<Map<String, Object>> sqlSelectList(String sql);

    /**
     * 查询数据返回
     *
     * @param sql   sql语句
     * @param value 参数
     * @return List<Map < String, Object>>
     */
    List<Map<String, Object>> sqlSelectList(String sql, Object value);

    /**
     * 查询数据返回
     *
     * @param sql        sql语句
     * @param resultType 具体类型
     * @return List<T>
     */    <T> List<T> sqlSelectList(String sql, Class<T> resultType);

    /**
     * 查询数据返回
     *
     * @param sql        sql语句
     * @param value      参数
     * @param resultType 具体类型
     * @return List<T>
     */    <T> List<T> sqlSelectList(String sql, Object value, Class<T> resultType);

    /**
     * 插入数据
     *
     * @param sql sql语句
     * @return int
     */
    int sqlInsert(String sql);

    /**
     * 插入数据
     *
     * @param sql   sql语句
     * @param value 参数
     * @return int
     */
    int sqlInsert(String sql, Object value);

    /**
     * 更新数据
     *
     * @param sql sql语句
     * @return int
     */
    int sqlUpdate(String sql);

    /**
     * 更新数据
     *
     * @param sql   sql语句
     * @param value 参数
     * @return int
     */
    int sqlUpdate(String sql, Object value);

    /**
     * 删除数据
     *
     * @param sql sql语句
     * @return int
     */
    int sqlDelete(String sql);

    /**
     * 查询数据返回List<T>
     *
     * @param sql   sql语句
     * @param value 参数
     * @return int
     */
    int sqlDelete(String sql, Object value);


}

 

新建一个SqlMapper 实现SqlBaseMapper接口


  SqlMapper 
     .sqlSession = sqlSessionFactory.openSession(.msUtils =  <T> T getOne(List<T> (list.size() == 1 list.get(0  (list.size() > 1  TooManyResultsException("Expected one result (or null) to be returned by selectOne(), but found: " +  Map<String, Object><Map<String, Object>> list =  (Map)  Map<String, Object><Map<String, Object>> list =  (Map)  <T> T sqlSelectOne(String sql, Class<T><T> list =   <T> T sqlSelectOne(String sql, Object value, Class<T><T> list =   List<Map<String, Object>>=   List<Map<String, Object>><?> parameterType = value !=  ? value.getClass() : =   <T> List<T> sqlSelectList(String sql, Class<T> (resultType == = =   <T> List<T> sqlSelectList(String sql, Object value, Class<T><?> parameterType = value !=  ? value.getClass() :  (resultType == = =   =   <?> parameterType = value !=  ? value.getClass() : =   =   <?> parameterType = value !=  ? value.getClass() : =   =   <?> parameterType = value !=  ? value.getClass() : =  
     .configuration =.languageDriver == "."  .configuration.hasStatement(msId,   newSelectMappedStatement(String msId, SqlSource sqlSource,  Class<?>= ( MappedStatement.Builder(.configuration, msId, sqlSource, SqlCommandType.SELECT)).resultMaps( ArrayList<ResultMap>.add(( org.apache.ibatis.mapping.ResultMap.Builder(com.culturalCenter.placeManage.mapper.SqlMapper.MSUtils..configuration, "defaultResultMap", resultType,  ArrayList(0 = ( MappedStatement.Builder(.configuration, msId, sqlSource, sqlCommandType)).resultMaps( ArrayList<ResultMap>.add(( org.apache.ibatis.mapping.ResultMap.Builder(com.culturalCenter.placeManage.mapper.SqlMapper.MSUtils..configuration, "defaultResultMap", Integer.TYPE,  ArrayList(0=  (=  StaticSqlSource(.newSelectMappedStatement(msId, sqlSource, Map. String selectDynamic(String sql, Class<?>= .newMsId(sql + (= .languageDriver.createSqlSource(.newSelectMappedStatement(msId, sqlSource, Map. String select(String sql, Class<?>= .newMsId(resultType + (=  StaticSqlSource( String selectDynamic(String sql, Class<?> parameterType, Class<?>= .newMsId(resultType + sql + (= .languageDriver.createSqlSource(=  (=  StaticSqlSource( String insertDynamic(String sql, Class<?>= .newMsId(sql + (= .languageDriver.createSqlSource(=  (=  StaticSqlSource( String updateDynamic(String sql, Class<?>= .newMsId(sql + (= .languageDriver.createSqlSource(=  (=  StaticSqlSource( String deleteDynamic(String sql, Class<?>= .newMsId(sql + (= .languageDriver.createSqlSource(

 

然后做一个 数据连接工厂类

SqlSessionFactoryConfig
/**
 * @author chaild
 * @Date 2020年6月23日18:25:22
 *  创建SQL连接工厂类
 * */@Configurationpublic class SqlSessionFactoryConfig {
    @javax.annotation.Resource
    DruidDataSource dataSource;

    @Bean
    @Primary    public SqlSessionFactory sqlSessionFactory() throws Exception {
        SqlSessionFactoryBean bean = new SqlSessionFactoryBean();
        bean.setDataSource(dataSource);//更多参数请自行注入
        bean.setPlugins(new Interceptor[]{new SqlInterceptor()});
        Resource[] resources = new PathMatchingResourcePatternResolver()
                .getResources("classpath*:mapper/*.xml");
        bean.setMapperLocations(resources);        return bean.getObject();
    }
}

 

使用示例:


@Autowired 
private SqlMapper sqlMapper;

###selectList//查询,返回List<Map>List<Map<String, Object>> list = sqlMapper.selectList("select * from country where id < 11");//查询,返回指定的实体类List<Country> countryList = sqlMapper.selectList("select * from country where id < 11", Country.class);//查询,带参数countryList = sqlMapper.selectList("select * from country where id < #{id}", 11, Country.class);//复杂点的查询,这里参数和上面不同的地方,在于传入了一个对象Country country = new Country();
country.setId(11);
countryList = sqlMapper.selectList("<script>" +
        "select * from country " +
        "   <where>" +
        "       <if test=\"id != null\">" +
        "           id &lt; #{id}" +
        "       </if>" +
        "   </where>" +
        "</script>", country, Country.class);
##复杂查询使用map传入参数   
 Map<String,String> map=new HashMap<>();
map.put("id","21321312312312312");
map.put("status","1");
sqlMapper.sqlSelectList("select * from tb_admin where id=#{id} and status=#{status}",map,Admin.class);


###selectOne 查询单条数据

Map<String, Object> map = sqlMapper.selectOne("select * from country where id = 35");

map = sqlMapper.selectOne("select * from country where id = #{id}", 35);

Country country = sqlMapper.selectOne("select * from country where id = 35", Country.class);

country = sqlMapper.selectOne("select * from country where id = #{id}", 35, Country.class);
###insert,update,delete

###insert 插入数据int result = sqlMapper.insert("insert into country values(1921,'天朝','TC')");

Country tc = new Country();
tc.setId(1921);
tc.setCountryname("天朝");
tc.setCountrycode("TC");//注意这里的countrycode和countryname故意写反的result = sqlMapper.insert("insert into country values(#{id},#{countrycode},#{countryname})"
                          , tc);


###update 更新使用
result = sqlMapper.update("update country set countryname = '天朝' where id = 35");

tc = new Country();
tc.setId(35);
tc.setCountryname("天朝");int result = sqlMapper.update("update country set countryname = #{countryname}" +
           " where id in(select id from country where countryname like 'A%')", tc);


##delete 删除使用
result = sqlMapper.delete("delete from country where id = 35");
result = sqlMapper.delete("delete from country where id = #{id}", 35);

 

 

如果实现 了 Interceptor 类进行SQL二次处理封装,会报二次编译的问题 


本文作者:admin

本文链接:https://www.javalc.com/post/16.html

版权声明:本篇文章于2020-09-25,由admin发表,转载请注明出处:分享你我。如有疑问,请联系我们

.net core 使用FluentFTP 实现FTP上传、下载文件

发表评论

取消
扫码支持