springmore-讓編程更容易


這是我多年項目的總結,並將其抽象出來,形成一個開源的項目
部分借鑒springside,將更多的實踐總結進來
基於spring+ibatis+springMVC
springmore-core專注於一些核心的應用,目前是讀寫分離組件
springmore-commons實用工具類
如:StringUtil,FileUtil,DateUtil,HTTPClientUtil,FTPUtil,DesUtil,ExcelUtil,XMLUtil等等
工具類這塊,我希望越來越完善,如果大家有什么建議,或者補充的,盡管提出來,我盡量滿足大家
真心希望得到大家的反饋

springmore-redis 封裝Jedis
gitcafe地址:https://gitcafe.com/tangyanbo/springmore(最新更新)
github地址:https://github.com/tangyanbo/springmore

springmore討論qq群261502547

springmore-core

spring+ibatis實現讀寫分離

  • 特點
    無縫結合spring+ibatis,對於程序員來說,是透明的
    除了修改配置信息之外,程序的代碼不需要修改任何東西
    支持spring的容器事務

  • 規則:

  1. 基於spring配置的容器事務
  2. 讀寫事務到主庫
  3. 只讀事務到從庫
  4. 如果沒有配置事務,更新語句全部到主庫,查詢語句均衡到從庫
  • 快速入門
    maven依賴
<dependency>
	<groupId>org.springmore</groupId>
	<artifactId>springmore-core</artifactId>
	<version>1.0.0</version>
</dependency>

dataSource配置(applicationContext.xml中)

<?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:context="http://www.springframework.org/schema/context"
	xsi:schemaLocation="
		http://www.springframework.org/schema/beans 
		http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
		http://www.springframework.org/schema/context
		http://www.springframework.org/schema/context/spring-context-3.0.xsd
		">
	<!-- C3P0連接池配置 -->
	<bean id="master" class="com.mchange.v2.c3p0.ComboPooledDataSource">
		<property name="driverClass">
			<value>com.mysql.jdbc.Driver</value>
		</property>
		<property name="jdbcUrl">
			<value>jdbc:mysql://192.168.1.246:3306/db1</value>
		</property>
		<property name="user">
			<value>ysb</value>
		</property>
		<property name="password">
			<value>ysb</value>
		</property>
		<property name="initialPoolSize">
			<value>20</value>
		</property>
		<property name="minPoolSize">
			<value>20</value>
		</property>
		<property name="maxPoolSize">
			<value>200</value>
		</property>
		<property name="maxIdleTime">
			<value>255000</value>
		</property>
	</bean>
	
	<bean id="dataSource2" class="com.mchange.v2.c3p0.ComboPooledDataSource">
		<property name="driverClass">
			<value>com.mysql.jdbc.Driver</value>
		</property>
		<property name="jdbcUrl">
			<value>jdbc:mysql://192.168.1.246:3306/db2</value>
		</property>
		<property name="user">
			<value>ysb</value>
		</property>
		<property name="password">
			<value>ysb</value>
		</property>
		<property name="initialPoolSize">
			<value>20</value>
		</property>
		<property name="minPoolSize">
			<value>20</value>
		</property>
		<property name="maxPoolSize">
			<value>200</value>
		</property>
		<property name="maxIdleTime">
			<value>255000</value>
		</property>
	</bean>
	
	<bean id="dataSource3" class="com.mchange.v2.c3p0.ComboPooledDataSource">
		<property name="driverClass">
			<value>com.mysql.jdbc.Driver</value>
		</property>
		<property name="jdbcUrl">
			<value>jdbc:mysql://192.168.1.246:3306/db3</value>
		</property>
		<property name="user">
			<value>ysb</value>
		</property>
		<property name="password">
			<value>ysb</value>
		</property>
		<property name="initialPoolSize">
			<value>20</value>
		</property>
		<property name="minPoolSize">
			<value>20</value>
		</property>
		<property name="maxPoolSize">
			<value>200</value>
		</property>
		<property name="maxIdleTime">
			<value>255000</value>
		</property>
	</bean>
	
	<bean id="dataSource" class="org.springmore.core.datasource.DynamicDataSource">
		<property name="master" ref="master" />		
		<property name="slaves">
			<list>
				<ref bean="dataSource2"/>
				<ref bean="dataSource3"/>
			</list>			
		</property>
	</bean>
</beans>

整合mybatis配置(applicationContext.xml中)

	<!-- ibatis3 工廠類 -->
	<bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
		<property name="dataSource" ref="dataSource" />
		<property name="configLocation" value="classpath:sqlMapConfig.xml" />
	</bean>
	<bean id="sqlSessionTemplate" class="org.mybatis.spring.SqlSessionTemplate">
		<constructor-arg index="0" ref="sqlSessionFactory" />
	</bean>
	
	<bean id="dynamicSqlSessionTemplate" class="org.springmore.core.datasource.DynamicSqlSessionTemplate">
		<constructor-arg index="0" ref="sqlSessionTemplate" />
	</bean>

事務配置(applicationContext.xml中)

	<!-- 定義單個jdbc數據源的事務管理器 -->
	<bean id="transactionManager"
		class="org.springmore.core.datasource.DynamicDataSourceTransactionManager">
		<property name="dataSource" ref="dataSource" />
	</bean>
	<!-- 以 @Transactional 標注來定義事務  -->
	<tx:annotation-driven transaction-manager="transactionManager"
		proxy-target-class="true" />		
	
	<!-- 配置事務的傳播特性 -->
	<tx:advice id="txAdvice" transaction-manager="transactionManager">
		<tx:attributes>
			<tx:method name="insert*" propagation="REQUIRED" read-only="false"
				rollback-for="Exception" />
			<tx:method name="delete*" propagation="REQUIRED" read-only="false"
				rollback-for="Exception" />
			<tx:method name="update*" propagation="REQUIRED" read-only="false"
				rollback-for="Exception" />
			<tx:method name="proc*" propagation="REQUIRED" read-only="false"
				rollback-for="Exception" />
			<tx:method name="select*" read-only="true" />
			<tx:method name="*" read-only="false" />
			<!-- <tx:method name="*" read-only="true" /> -->
		</tx:attributes>
	</tx:advice>
	<!-- 那些類的哪些方法參與事務 -->
	<aop:config>
		<aop:pointcut id="allManagerMethod" expression="execution(* org.springmore.core.dao..*(..))" />
		<aop:advisor pointcut-ref="allManagerMethod" advice-ref="txAdvice" />
	</aop:config>

dao代碼示例:

@Repository("UserMapperImpl")
public class UserMapperImpl implements UserMapper{
	
	@Autowired
	private DynamicSqlSessionTemplate sqlSessionTemplate;

	//從庫
	public List<User> selectByUserNameAndPwd(User user) {
		return sqlSessionTemplate.selectList("selectByUserNameAndPwd", user);
	}

	//主庫
	public void insert(User user) {	
		sqlSessionTemplate.insert("insert", user);		
	}
}

springmore-redis組件

  • 封裝jedis客戶端
  1. 使客戶端調用更加簡單
    如:JedisTemplate負責對Jedis連接的獲取與歸還
  2. 分布式JedisShardedTemplate改用一致性哈希算法存取

JedisTemplate代碼示例(用於非分布式部署的redis)
初始化jedisTemplate,客戶端可以將該部分代碼封裝到工廠類中

HostAndPort address = new HostAndPort("192.168.1.245",6380);
JedisPoolConfig config = new JedisPoolConfig();
JedisPool jedisPool = new JedisDirectPool("pool", address, config);
jedisTemplate = new JedisTemplate(jedisPool);

調用方法:JedisTemplate負責對Jedis連接的獲取與歸還

jedisTemplate.set("key", "value");

JedisShardedTemplate代碼示例(用於分布式部署的redis)
初始化JedisShardedTemplate

HostAndPort address1 = new HostAndPort("192.168.1.245",6380);
HostAndPort address2 = new HostAndPort("192.168.1.246",6380);
JedisPoolConfig config = new JedisPoolConfig();
JedisPool jedisPool1 = new JedisDirectPool("pool1", address1, config);
JedisPool jedisPool2 = new JedisDirectPool("pool2", address2, config);
jedisTemplate = new JedisShardedTemplate(new JedisPool[] { jedisPool1, jedisPool2 });

調用方法

jedisTemplate.set("key", "value");

springmore-commons組件

HttpClientUtil

功能:http以及https
基於最新的httpcomponents包實現
get請求,返回String報文,返回的報文默認是UTF-8編碼
如果需要制定編碼,可傳入編碼參數

@Test
public void testDoGetStringString() throws Exception {
	String doGet = HttpClientUtil.get("http://localhost:8888/login/");
	String doGet2 = HttpClientUtil.get("http://localhost:8888/login/",HttpClientUtil.UTF_8);
	System.out.println(doGet);
}	

post請求,可以制定編碼

@Test
public void testDoPost() throws Exception {
	List <NameValuePair> nvps = new ArrayList <NameValuePair>();
    nvps.add(new BasicNameValuePair("user.userName", "哈哈"));
	String doGet = HttpClientUtil.post("http://localhost:8888/login/login!login.ac",nvps);
	System.out.println(doGet);
}

ssl請求

@Test
public void testSSL() throws Exception {
	String doGet = HttpClientUtil.getSSL("https://www.baidu.com", "utf-8");
	List <NameValuePair> nvps = new ArrayList <NameValuePair>();
    nvps.add(new BasicNameValuePair("user.userName", "哈哈"));
	String post = HttpClientUtil.postSSL("https://www.baidu.com", nvps,"utf-8");
	System.out.println(post);
}	

文件上傳和下載

@Test
public void getFile() throws Exception{
	HttpClientUtil.getFile("http://localhost:8888/login/login!login.ac");
	HttpClientUtil.postFile("http://localhost:8888/login/login!login.ac", "fileName", new File("d:/test.txt"));
}

org.springmore.commons.codec:

  • Base64.java Base64編碼與解碼

org.springmore.commons.io:

  • ExcelUtil excel文件讀寫
  • FileUtil 文件讀寫
  • FTPUtil ftp操作
  • ResourceUtil
  • XMLUtil dom4j jaxb封裝
  • ImageUtil 圖片縮放,切割封裝

org.springmore.commons.lang

  • ArrayUtil
  • HexUtil 字符字節十六進制轉換
  • StringUtil
  • DateUtil

org.springmore.commons.security

  • DESedeUtil 3des加密
  • DESUtil 單des加密
  • Md5Util md5加密
  • RSAUtil rsa加密

org.springmore.commons.web

  • HttpClientUtil http https封裝
  • WebUtil servlet發送response信息封裝,發送json字符串封裝


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2026 CODEPRJ.COM