ssm开发mybatis全局变量配置和转义符的使用

mybatis当中定义全局变量

在开发过程当中难免有些固定的常量,比如固定的url开头,我们需要在xml文件当中的sql语句中进行拼接变量。如果每次都用传递参数的方法进行使用的话,那样无疑会增加工作量,因此我们需要mybatis的配置文件当中添加全局变量。

  • 首先确定实例化sqlSessionFactory的时候添加mybatis的配置文件
  • 在mybatis的配置文件当中添加全局变量
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE configuration PUBLIC "-//mybatis.org//DTD SQL Map Config 3.4//EN"  
    "http://mybatis.org/dtd/mybatis-3-config.dtd">
<configuration>
    <!--name代表添加的全局属性名;value代表添加的属性值;-->
    <properties>
            <property name="urlpath" value="http://172.xx.x.xx:8080/upload/"/>
    </properties>
    <settings>
            <!-- 这个配置使全局的映射器启用或禁用缓存 -->
            <setting name="cacheEnabled" value="true" />
            <!-- 全局启用或禁用延迟加载。当禁用时,所有关联对象都会即时加载 -->
            <setting name="lazyLoadingEnabled" value="true" />
            <setting name="multipleResultSetsEnabled" value="true" />
            <setting name="useColumnLabel" value="true" />
            <!--<setting name="defaultStatementTimeout" value="25000" />-->
            <!--<setting name="defaultExecutorType" value="REUSE" />-->
            <setting name="mapUnderscoreToCamelCase" value="true"/>
            <setting name="logImpl" value="STDOUT_LOGGING" />
    </settings>
</configuration>
  • 接下来在写sql语句的时候便可以使用这个全局变量
<select id="getCourseInCategory" resultType="java.util.Map">
          SELECT 
            t.companyId,
            t.courseId,
            userCd,
            t.categoryId,
            courseName,
            courseMemo,
            courseType,
            subCourseType,
            CONCAT(&apos;${urlpath}&apos;,courseImg) as imgURL,
            courseCredit,
            courseScore,
            courseSee,
            t.status,
            t.creator,
            t.createtime,
            t.modifier,
            t.modifytime, 
            e.userCount,
            (SELECT count(*) from newminicollege.t_m_subcourse  s 
                WHERE t.companyId=s.companyId and  t.courseId=s.courseId and s.status=1 and s.audit=2
             ) AS  subCount
            FROM newminicollege.t_m_course t
            left JOIN(
                    SELECT  a.courseId,count(DISTINCT j.userCd)as userCount from newminicollege.t_m_course a
                    left JOIN newminicollege.t_u_joincourse j 
                    on a.companyId=#{companyId}
                    and  a.courseId=j.joinCourseId
                    group by a.courseId
                ) e
            ON t.courseId=e.courseId
            WHERE t.companyId=#{companyId} AND subCourseType=#{categoryId} and  t.status=1 and  t.courseSee in('公共')
</select>

xml中sql语句转义符的使用

xml中转义符对照表

实体名称显示结果描述
&lt;<大于号
&gt;>小于号
&amp;&
&apos;单引号
&quot;双引号

在上面的语句当中就使用到了转义符

CONCAT(&apos;${urlpath}&apos;,courseImg) as imgURL
正常显示应该为CONCAT('xxx.xxx.xx.xxx/upload/') as imgURL

同样的当在代码当中用到大于号或者小于号的时候,如下

select * from  user where  usercd &gt;= 10115208 and age &lt;= 40

上面语句也可以使用<![CDATA[内容]]>这种方式进行书写,作用是让[]里面的内容在xml中会原模原样的显示

<![CDATA[ select * from  user where  usercd >= 18 and age <= 40 ]]>

当然转义字符在ssm中使用很广泛,在开发过程当中使用<![CDATA[内容]]>更加方便快捷,也不需要查询转义字符对照表

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
SSM框架使用MyBatis,需要进行以下步骤: 1. 引入MyBatis的依赖。在Maven项目,在pom.xml文件添加以下依赖: ```xml <dependency> <groupId>org.mybatis</groupId> <artifactId>mybatis</artifactId> <version>3.4.6</version> </dependency> ``` 2. 配置数据源。在Spring的配置文件配置数据源,建议使用连接池,可以提高数据库连接的效率。 ```xml <bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource"> <property name="driverClass" value="${jdbc.driverClass}" /> <property name="jdbcUrl" value="${jdbc.jdbcUrl}" /> <property name="user" value="${jdbc.user}" /> <property name="password" value="${jdbc.password}" /> <property name="maxPoolSize" value="${jdbc.maxPoolSize}" /> <property name="minPoolSize" value="${jdbc.minPoolSize}" /> <property name="initialPoolSize" value="${jdbc.initialPoolSize}" /> <property name="maxIdleTime" value="${jdbc.maxIdleTime}" /> </bean> ``` 3. 配置MyBatis的SqlSessionFactory。SqlSessionFactory是MyBatis的核心类,用于创建SqlSession对象。 ```xml <bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean"> <property name="dataSource" ref="dataSource" /> <property name="typeAliasesPackage" value="com.example.model" /> <property name="mapperLocations" value="classpath*:mappers/*.xml" /> </bean> ``` 4. 配置MyBatis的MapperScannerConfigurer。MapperScannerConfigurer是MyBatis提供的扫描Mapper接口的类,用于自动创建Mapper接口的实现类。 ```xml <bean id="mapperScannerConfigurer" class="org.mybatis.spring.mapper.MapperScannerConfigurer"> <property name="basePackage" value="com.example.dao" /> </bean> ``` 5. 创建Mapper接口。在com.example.dao包下创建Mapper接口,使用注解或者XML文件定义SQL语句。 ```java public interface UserMapper { @Select("select * from user where id=#{id}") User selectById(int id); } ``` 6. 使用Mapper接口。在Service层注入Mapper接口,调用Mapper方法。 ```java @Service public class UserServiceImpl implements UserService { @Autowired private UserMapper userMapper; public User selectById(int id) { return userMapper.selectById(id); } } ``` 以上就是SSM框架使用MyBatis的基本流程。

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值