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>
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); }
鼓励:觉得写得有帮助就支付宝扫一下吧,对你没有损失,也给我动力