1.可以通过配置文件

复制代码
<?xml version="1.0" encoding="UTF-8"?>  
<beans xmlns="http://www.springframework.org/schema/beans"  
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:p="http://www.springframework.org/schema/p"  
    xmlns:context="http://www.springframework.org/schema/context"  
    xmlns:mvc="http://www.springframework.org/schema/mvc"  
    xsi:schemaLocation="http://www.springframework.org/schema/beans    
                        http://www.springframework.org/schema/beans/spring-beans-4.0.xsd    
                        http://www.springframework.org/schema/context    
                        http://www.springframework.org/schema/context/spring-context-4.0.xsd    
                        http://www.springframework.org/schema/mvc                            http://www.springframework.org/schema/mvc/spring-mvc-4.0.xsd">  
    <!-- 自动扫描 -->  
    <context:component-scan base-package="gamedataserver"></context:component-scan>
    <!-- 引入配置文件 -->  
    <bean id="propertyConfigurer"  
        class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">  
        <property name="location" value="classpath:jdbc.properties" />  
    </bean>  
  
    <bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource"  
        destroy-method="close">  
        <property name="driverClassName" value="${driver}" />  
        <property name="url" value="${url}" />  
        <property name="username" value="${username}" />  
        <property name="password" value="${password}" />  
        <!-- 初始化连接大小 -->  
        <property name="initialSize" value="${initialSize}"></property>  
        <!-- 连接池最大数量 -->  
        <property name="maxActive" value="${maxActive}"></property>  
        <!-- 连接池最大空闲 -->  
        <property name="maxIdle" value="${maxIdle}"></property>  
        <!-- 连接池最小空闲 -->  
        <property name="minIdle" value="${minIdle}"></property>  
        <!-- 获取连接最大等待时间 -->  
        <property name="maxWait" value="${maxWait}"></property>  
    </bean> 
    
       <bean id="dataSourceEx" class="org.apache.commons.dbcp.BasicDataSource" destroy-method="close">  
         <!-- 数据库基本信息配置 -->
         <property name="driverClassName" value="${driver}" />  
        <property name="url" value="${urlEx}" />  
        <property name="username" value="${usernameEx}" />  
        <property name="password" value="${passwordEx}" />  
        <!-- 初始化连接大小 -->  
        <property name="initialSize" value="${initialSize}"></property>  
        <!-- 连接池最大数量 -->  
        <property name="maxActive" value="${maxActive}"></property>  
        <!-- 连接池最大空闲 -->  
        <property name="maxIdle" value="${maxIdle}"></property>  
        <!-- 连接池最小空闲 -->  
        <property name="minIdle" value="${minIdle}"></property>  
        <!-- 获取连接最大等待时间 -->  
        <property name="maxWait" value="${maxWait}"></property>  
    </bean>
    
    <bean id="dataSourceThree" class="org.apache.commons.dbcp.BasicDataSource" destroy-method="close">  
         <!-- 数据库基本信息配置 -->
         <property name="driverClassName" value="${driver}" />  
        <property name="url" value="${urlThree}" />  
        <property name="username" value="${usernameThree}" />  
        <property name="password" value="${passwordThree}" />  
        <!-- 初始化连接大小 -->  
        <property name="initialSize" value="${initialSize}"></property>  
        <!-- 连接池最大数量 -->  
        <property name="maxActive" value="${maxActive}"></property>  
        <!-- 连接池最大空闲 -->  
        <property name="maxIdle" value="${maxIdle}"></property>  
        <!-- 连接池最小空闲 -->  
        <property name="minIdle" value="${minIdle}"></property>  
        <!-- 获取连接最大等待时间 -->  
        <property name="maxWait" value="${maxWait}"></property>  
    </bean>
  
    <!-- spring和MyBatis完美整合,不需要mybatis的配置映射文件          第一个数据库链接-->  
    <bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">  
        <property name="dataSource" ref="dataSource" />  
        <!-- 自动扫描mapping.xml文件 -->  
        <property name="mapperLocations" value="classpath:gamedataserver/mapping/one/*.xml"></property>  
    </bean>  
    
    
      <!-- spring和MyBatis完美整合,不需要mybatis的配置映射文件          第二个数据库链接 -->  
    <bean id="sqlSessionFactoryEx" class="org.mybatis.spring.SqlSessionFactoryBean">
        <property name="dataSource" ref="dataSourceEx" />
        <!-- mapper扫描 -->
         <property name="mapperLocations" value="classpath:gamedataserver/mapping/two/*.xml"></property> 
    </bean>

     <!-- spring和MyBatis完美整合,不需要mybatis的配置映射文件          第二个数据库链接 -->  
    <bean id="sqlSessionFactoryThree" class="org.mybatis.spring.SqlSessionFactoryBean">
        <property name="dataSource" ref="dataSourceThree" />
        <!-- mapper扫描 -->
         <property name="mapperLocations" value="classpath:gamedataserver/mapping/three/*.xml"></property> 
    </bean>
  
    <!-- DAO接口所在包名,Spring会自动查找其下的类 -->  
    <bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">  
        <property name="basePackage" value="gamedataserver.dao.one" />  
        <property name="sqlSessionFactoryBeanName" value="sqlSessionFactory"></property>  
    </bean>
    
     <!-- DAOEx接口所在包名,Spring会自动查找其下的类 -->  
    <bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">  
        <property name="basePackage" value="gamedataserver.dao.two" />  
        <property name="sqlSessionFactoryBeanName" value="sqlSessionFactoryEx"></property>  
    </bean>
    
    <!-- DAOEx接口所在包名,Spring会自动查找其下的类 -->  
    <bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">  
        <property name="basePackage" value="gamedataserver.dao.three" />  
        <property name="sqlSessionFactoryBeanName" value="sqlSessionFactoryThree"></property>  
    </bean>
  
    <!-- (事务管理)transaction manager, use JtaTransactionManager for global tx -->

    <bean name="transactionManagerEx"
        class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
        <property name="dataSource" ref="dataSourceEx"></property>
    </bean>
    <bean id="transactionManager"
        class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
        <property name="dataSource" ref="dataSource" />
    </bean>
    <bean id="transactionManagerThree"
        class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
        <property name="dataSource" ref="dataSourceThree" />
    </bean>
    
     <!-- 配置自定义Realm -->
    <bean id="myRealm" class="shiro.MyRealm"/>
    
    
    
   
    
    
    <!-- 安全管理器 -->
    <bean id="securityManager" class="org.apache.shiro.web.mgt.DefaultWebSecurityManager">
        <property name="realm" ref="myRealm"/>
    </bean>

    <!-- Shiro过滤器 核心-->
    <bean id="shiroFilter" class="org.apache.shiro.spring.web.ShiroFilterFactoryBean">
        <!-- Shiro的核心安全接口,这个属性是必须的 -->
        <property name="securityManager" ref="securityManager"/>
        <!-- 身份认证失败,则跳转到登录页面的配置 -->
        <property name="loginUrl" value="/jsp/admin/loginIndex.jsp"/>
        <!-- 权限认证失败,则跳转到指定页面 -->
        <property name="unauthorizedUrl" value="/jsp/admin/nopower.jsp"/>
        <!-- Shiro连接约束配置,即过滤链的定义 -->
        <property name="filterChainDefinitions">
            <value>
                <!--anon 表示匿名访问,不需要认证以及授权-->
                /css/=anon
                /js/=anon
                /fonts/=anon
                /img/=anon
                /loginIndex=anon
                
                <!--authc表示需要认证 没有进行身份认证是不能进行访问的-->
                /jsp/sysconfig/*=authc,roles[superadmin]
                /jsp/moneyRecord/*=authc
                /jsp/index.jsp=authc,roles[superadmin]
                /jsp/index_player.jsp=authc,roles[admin]
            </value>
        </property>
    </bean>

    <!-- 保证实现了Shiro内部lifecycle函数的bean执行 -->
    <bean id="lifecycleBeanPostProcessor" class="org.apache.shiro.spring.LifecycleBeanPostProcessor"/>

    <!-- 开启Shiro注解 -->
    <bean class="org.springframework.aop.framework.autoproxy.DefaultAdvisorAutoProxyCreator" depends-on="lifecycleBeanPostProcessor"/>
    <bean class="org.apache.shiro.spring.security.interceptor.AuthorizationAttributeSourceAdvisor">
        <property name="securityManager" ref="securityManager"/>
    </bean>
</beans>  
复制代码

这里有三个数据源

2.可以通过注解(不需要mapping.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="mybatis/jdbc.properties"/> 
--> 

<typeAliases> 
<typeAlias alias="Player" type="gamedataserver.model.Player"/>
<typeAlias alias="ServerConfig" type="gamedataserver.model.ServerConfig"/>
</typeAliases> 

<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> 
<mapper class="gamedataserver.dao.one.PlayerMapper"/> 
<mapper class="gamedataserver.dao.two.ServerConfigMapper"/> 
</mappers>
</configuration>
复制代码

 

 

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
package gamedataserver.dao.one;
 
import java.util.List;
import java.util.Map;
 
import org.apache.ibatis.annotations.Delete;
import org.apache.ibatis.annotations.Select;
import org.apache.ibatis.annotations.Update;
 
import gamedataserver.model.Player;
 
public interface PlayerMapper {
    @Delete( "DELETE FROM tbl_Player where id=#{id}")
    int deleteByPrimaryKey(Long id);
 
    int insert(Player record);
 
    int insertSelective(Player record);
    @Select("SELECT * FROM tbl_Player where id=#{id}")
    Player selectByPrimaryKey(Long id);
     
    int updateByPrimaryKeySelective(Player record);
    @Update("update tbl_Player set UserName = #{username,jdbcType=VARCHAR} ,UserPass = #{userpass,jdbcType=VARCHAR},"
      +"SessionKey = #{sessionkey,jdbcType=VARCHAR},"
      +"GameMoney = #{gamemoney,jdbcType=BIGINT},"
      +"RealName = #{realname,jdbcType=VARCHAR},"
      +"OpenRoomFreeTimes = #{openroomfreetimes,jdbcType=INTEGER},"
      +"IsMember = #{ismember,jdbcType=INTEGER},"
      +"UnionId = #{unionid,jdbcType=VARCHAR},"
      +"HeadUrl = #{headurl,jdbcType=VARCHAR},"
      +"Sex = #{sex,jdbcType=INTEGER},"
      +"Diamond = #{diamond,jdbcType=INTEGER},"
      +"GoldenFlowerCard = #{goldenflowercard,jdbcType=INTEGER},"
      +"BullCard = #{bullcard,jdbcType=INTEGER},"
      +"MahjongCard = #{mahjongcard,jdbcType=INTEGER},"
      +"LoginTime = #{logintime,jdbcType=BIGINT},"
      +"LogoutTime = #{logouttime,jdbcType=BIGINT},"
      +"CreateTime = #{createtime,jdbcType=BIGINT},"
      +"IsOnLine = #{isonline,jdbcType=INTEGER},"
      +"Version = #{version,jdbcType=VARCHAR} "
      +"where ID = #{id,jdbcType=BIGINT}")
    int updateByPrimaryKey(Player record);
    @Select("SELECT * FROM tbl_Player")
    public List<Player> selectAllPlayer();
    //这个是start limit
    List<Player> selectPlayerByStartLimit(Map params);
    //这个是 where
    int selectPlayerByWhere(Map params);
    @Select("SELECT count(*) FROM tbl_Player where CreateTime>=#{timeStartStamp} and CreateTime<=#{timeEndStamp}")
    int selectPlayerCountByCreateTime(Map<String,Long> map);
}

 

鼓励:觉得写得有帮助就支付宝扫一下吧,对你没有损失,也给我动力

posted on   我是坏男孩  阅读(23144)  评论(0编辑  收藏  举报
编辑推荐:
· .NET制作智能桌面机器人:结合BotSharp智能体框架开发语音交互
· 软件产品开发中常见的10个问题及处理方法
· .NET 原生驾驭 AI 新基建实战系列:向量数据库的应用与畅想
· 从问题排查到源码分析:ActiveMQ消费端频繁日志刷屏的秘密
· 一次Java后端服务间歇性响应慢的问题排查记录
阅读排行:
· 互联网不景气了那就玩玩嵌入式吧,用纯.NET开发并制作一个智能桌面机器人(四):结合BotSharp
· 一个基于 .NET 开源免费的异地组网和内网穿透工具
· 《HelloGitHub》第 108 期
· Windows桌面应用自动更新解决方案SharpUpdater5发布
· 我的家庭实验室服务器集群硬件清单
< 2025年3月 >
23 24 25 26 27 28 1
2 3 4 5 6 7 8
9 10 11 12 13 14 15
16 17 18 19 20 21 22
23 24 25 26 27 28 29
30 31 1 2 3 4 5
点击右上角即可分享
微信分享提示