Mybatis中xml文件配置sql标准格式

1.Mapper.xml

<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd" >
<mapper namespace="com.test.dao.StrategyDao">
<resultMap id="StrategyResultMap" type="com.test.entity.Strategy">
        <result property="id" column="id"/>
        <result property="projectId" column="project_id"/>
        <result property="strategyType" column="strategy_type"/>
        <result property="strategyName" column="strategy_name"/>
        <result property="alias" column="alias"/>
    </resultMap>
<select id="findNameList" resultType="java.util.Map" parameterType="com.test.entity.Strategy">
        SELECT DISTINCT t.strategy_name strategyName,t.alias from test_strategy t WHERE
        t.project_id=#{projectId, jdbcType=INTEGER} AND t.del_status=0 AND t.strategy_type =#{strategyType}
        AND (t.strategy_name LIKE concat('%',#{strategyName,jdbcType=VARCHAR},'%')
        or t.alias LIKE concat('%',#{strategyName,jdbcType=VARCHAR},'%'))
    </select>
</mapper>

2.Dao

@MyBatisRepository
public interface StrategyDao extends ICrudDao<Strategy, Integer> {
	 List<Map<String,Object>> findNameList(Strategy entity);
}

3.service

public List<Map<String, Object>> findNameList(Strategy strategy) throws Exception {
        try {
            return dao.findNameList(strategy);
        } catch (Exception var3) {
            throw new MySqlException("P2101", "数据库执行异常", var3);
        }
   }

-----------
public class MySqlException extends ServiceException {
    public MySqlException(String errorCode, Object[] args) {
        super(errorCode, args);
    }

    public MySqlException(String errorCode) {
        super(errorCode);
    }

    public MySqlException(String errorCode, String message) {
        super(errorCode, message);
    }

    public MySqlException(String errorCode, String message, Throwable cause) {
        super(errorCode, message, cause);
    }
}

4.Controller

    @RequestMapping("/nameList")
    public Map<String, Object> findNameList(@RequestBody Strategy strategy) throws Exception {
        try {
            return result(strategyService.findNameList(strategy));
        } catch (Exception e) {
            throw e;
        }
    }
-----------
public Map<String, Object> result(Object object) {
        Map<String, Object> result = new HashMap();
        ResultUtil.addSuccessResult(result, object);
        return result;
    }
-------
public class ResultUtil {
    public ResultUtil() {
    }

    public static void addSuccessResult(Map<String, Object> resultMap, Object data) {
        resultMap.put("result", "1");
        resultMap.put("msg", "调用成功!");
        resultMap.put("code", "200");
        resultMap.put("data", data);
    }
}
  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
MyBatis是一个开源的持久层框架,它可以将数据库操作与Java对象之间的映射关系进行配置,提供了一种简单且灵活的方式来访问数据库。 在MyBatisXML配置文件是配置映射关系的主要方式。下面是一个简单的MyBatis XML配置文件的示例: ```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> <environments default="development"> <environment id="development"> <transactionManager type="JDBC"/> <dataSource type="POOLED"> <property name="driver" value="com.mysql.jdbc.Driver"/> <property name="url" value="jdbc:mysql://localhost:3306/mybatis_demo"/> <property name="username" value="root"/> <property name="password" value="123456"/> </dataSource> </environment> </environments> <mappers> <mapper resource="com/example/mapper/UserMapper.xml"/> </mappers> </configuration> ``` 上述配置文件包含了以下几个重要的配置项: 1. `<environments>`:定义了环境配置,可以配置多个环境,通过`default`属性指定默认环境。 2. `<transactionManager>`:定义了事务管理器的类型,这里使用的是JDBC事务管理器。 3. `<dataSource>`:定义了数据源的类型和相关属性,这里使用的是连接池数据源(POOLED)。 4. `<mappers>`:定义了映射器(Mapper)的配置,可以通过`resource`属性指定映射器的XML文件路径。 在`<mappers>`标签,可以通过`<mapper>`标签来引入映射器的配置文件。下面是一个简单的映射器配置文件的示例: ```xml <?xml version="1.0" encoding="UTF-8"?> <!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd"> <mapper namespace="com.example.mapper.UserMapper"> <select id="getUserById" resultType="com.example.model.User"> SELECT * FROM user WHERE id = #{id} </select> <insert id="insertUser" parameterType="com.example.model.User"> INSERT INTO user (id, name, age) VALUES (#{id}, #{name}, #{age}) </insert> <update id="updateUser" parameterType="com.example.model.User"> UPDATE user SET name = #{name}, age = #{age} WHERE id = #{id} </update> <delete id="deleteUser" parameterType="int"> DELETE FROM user WHERE id = #{id} </delete> </mapper> ``` 上述映射器配置文件定义了一些SQL语句,通过`<select>`、`<insert>`、`<update>`和`<delete>`标签来配置对应的SQL操作。其,`id`属性指定了SQL语句的唯一标识,`parameterType`属性指定了参数类型,`resultType`属性指定了返回结果的类型。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值