摘要:項目主要是在框架下對雙數據源進行配置和操作的實例,關于框架搭建這里就不予展示與介紹了,想了解的請參考之前的項目介紹項目代碼獲取功能分類增刪改查雙數據源切換對類型和類型進行緩存插入與讀取項目結構代碼雙數據源配置相關類數據庫交互層實體對象層相
redis-mongo
項目主要是在spring、springmvc、mybatis框架下對mysql、mysql雙數據源、redis、mongo進行配置和操作的實例,關于框架搭建這里就不予展示與介紹了,想了解的請參考之前的shiro項目介紹
項目代碼獲取:https://github.com/pysasuke/r...
mysql增刪改查
mysql雙數據源切換
redis對String類型和Object類型進行緩存
mongo插入與讀取
項目結構 java:代碼core:雙數據源配置相關類
dao:數據庫交互層
entity:實體對象層
util:redis相關工具類
resources:配置文件mapping:mybatis與實體映射文件存放包
application.xml:spring配置文件入口,加載spring-config.xml
spring-mvc.xml:springmvc配置相關文件
spring-config.xml:加載其他集成的配置文件
spring-mybatis.xml:mybatis相關配置文件
spring-mongo.xml:mongo相關配置文件
spring-redis.xml:redis相關配置文件
db.properties:數據庫相關參數配置
log4j.properties:日志相關參數配置
redis.properties:redis相關參數配置
webapp:web相關web.xml
test:測試包doubleMysql:雙數據源測試包
@RunWith(SpringJUnit4ClassRunner.class) @ContextConfiguration("classpath:application.xml") //加載xml文件 public class TestDoubleMysql { @Autowired private ImuserMapper imuserMapper; @Autowired private TuserMapper tuserMapper; @Test public void test() { IMUser imUser = generateIMUser(); DataSourceContextHolder.setDBType(DataSourceType.DB_IM); int row_im = imuserMapper.insert(imUser); Assert.assertEquals(1, row_im); DataSourceContextHolder.clearDBType(); Tuser tuser = generateTuser(); DataSourceContextHolder.setDBType(DataSourceType.DB_ERP); int row_t = tuserMapper.insert(tuser); Assert.assertEquals(1, row_t); DataSourceContextHolder.clearDBType(); } private IMUser generateIMUser() { IMUser imUser = new IMUser(); imUser.setUsername("lisi"); imUser.setPassword("123456"); imUser.setNickname("huahua"); imUser.setEmail("123456@qq.com"); imUser.setMobile("18650140605"); return imUser; } private Tuser generateTuser() { Tuser tuser = new Tuser(); tuser.setUsername("zhangsan"); tuser.setPassword("123456"); tuser.setMobile("13666666666"); tuser.setEmail("123456@qq.com"); return tuser; } }
mongo:mongo測試包
@RunWith(SpringJUnit4ClassRunner.class) @ContextConfiguration("classpath:application.xml") public class TestMongo { @Resource private MongoTemplate mongoTemplate; @Test public void test() { MgUser mgUser = new MgUser(); mgUser.setId(1L); mgUser.setNikename("pysasuke"); mgUser.setPhone("18650140605"); //插入成功后_id屬性有值(mongo數據庫生成的) mongoTemplate.insert(mgUser); MgUser selectMgUser = mongoTemplate.findOne(new Query(Criteria.where("id").is(1L)), MgUser.class); Assert.assertEquals(mgUser.getId(), selectMgUser.getId()); } }
mysql:mysql測試包
@RunWith(SpringJUnit4ClassRunner.class) @ContextConfiguration("classpath:application.xml") //加載xml文件 public class TestMysql { @Autowired private TuserMapper tuserMapper; @Test public void testInsert() { Tuser tuser = generateTuser(); int row = tuserMapper.insert(tuser); Assert.assertEquals(1, row); } @Test public void testSelect() { Tuser tuser = generateTuser(); /* 插入成功后id會有值(mysql數據庫生成的) 前提是: 1.主鍵設置為自動增長 2.xml中insert語句設置有:useGeneratedKeys="true" keyProperty="id" */ int row = tuserMapper.insert(tuser); Assert.assertEquals(1, row); Tuser selectedTuser = tuserMapper.selectByPrimaryKey(tuser.getId()); Assert.assertNotNull(selectedTuser); Assert.assertEquals(tuser, selectedTuser); } @Test public void testUpdate() { Tuser tuser = generateTuser(); int row = tuserMapper.insert(tuser); Assert.assertEquals(1, row); Tuser selectedTuser = tuserMapper.selectByPrimaryKey(tuser.getId()); Assert.assertNotNull(selectedTuser); selectedTuser.setPassword("654321"); tuserMapper.updateByPrimaryKeySelective(selectedTuser); Tuser updatedTuser = tuserMapper.selectByPrimaryKey(selectedTuser.getId()); Assert.assertNotNull(updatedTuser); Assert.assertEquals(selectedTuser.getPassword(), updatedTuser.getPassword()); } @Test public void testDelete() { Tuser tuser = generateTuser(); int row = tuserMapper.insert(tuser); Assert.assertEquals(1, row); tuserMapper.deleteByPrimaryKey(tuser.getId()); Tuser selectedTuser = tuserMapper.selectByPrimaryKey(tuser.getId()); Assert.assertNull(selectedTuser); } private Tuser generateTuser() { Tuser tuser = new Tuser(); tuser.setUsername("zhangsan"); tuser.setPassword("123456"); tuser.setMobile("13666666666"); tuser.setEmail("123456@qq.com"); return tuser; } }
redis:redis測試包
@RunWith(SpringJUnit4ClassRunner.class) @ContextConfiguration("classpath:application.xml") public class TestRedis { @Autowired private RedisCache cache; @Test public void testPutString() { cache.putCacheWithExpireTime("testPutString", "123456", 60); } @Test public void tesGetString() { cache.putCacheWithExpireTime("tesGetString", "123456", 60); String str = cache.getCache("tesGetString", String.class); Assert.assertEquals("123456", str); } @Test public void testObject() { Person person = generatePerson(); cache.putCacheWithExpireTime("testObject:" + person.getUsername(), person, 600); } @Test public void tesGetObject() { Person person = generatePerson(); cache.putCacheWithExpireTime("tesGetObject:" + person.getUsername(), person, 600); Person cachePerson = cache.getCache("tesGetObject:"+person.getUsername(), Person.class); Assert.assertEquals(person, cachePerson); } @Test public void tesDeleteCache() { cache.putCacheWithExpireTime("tesDeleteCache", "123456", 60); String cacheStr = cache.getCache("tesDeleteCache", String.class); Assert.assertEquals("123456", cacheStr); cache.deleteCache("tesDeleteCache"); String DeleteStr = cache.getCache("tesDeleteCache", String.class); Assert.assertNull(DeleteStr); } private Person generatePerson() { Person person = new Person(); person.setId(1L); person.setUsername("wangwu"); person.setPassword("123456"); return person; } }其他文件 logs:日志存放 deploy:部署文件(sql)
update.sql
文章版權歸作者所有,未經允許請勿轉載,若此文章存在違規行為,您可以聯系管理員刪除。
轉載請注明本文地址:http://specialneedsforspecialkids.com/yun/19095.html
摘要:項目主要是在框架下對雙數據源進行配置和操作的實例,關于框架搭建這里就不予展示與介紹了,想了解的請參考之前的項目介紹項目代碼獲取功能分類增刪改查雙數據源切換對類型和類型進行緩存插入與讀取項目結構代碼雙數據源配置相關類數據庫交互層實體對象層相 redis-mongo 項目主要是在spring、springmvc、mybatis框架下對mysql、mysql雙數據源、redis、mongo進...
摘要:作者重慶森林鏈接來源牛客網整個三月份通過牛客網和網友分享的經驗學到了很多東西,現在反饋一下我的面試經歷,希望對同學們有幫助。個人情況大三本方向渣碩,經過實驗室學長內推,于三月底完成面試。校招是實力和運氣的結合,缺一不可。 歡迎關注我的微信公眾號:Java面試通關手冊(堅持原創,分享美文,分享各種Java學習資源,面試題,以及企業級Java實戰項目回復關鍵字免費領取):showImg(h...
閱讀 984·2021-11-24 09:39
閱讀 2185·2021-11-16 11:54
閱讀 2077·2021-11-11 17:22
閱讀 2372·2021-09-30 09:55
閱讀 3591·2021-08-12 13:22
閱讀 1626·2019-08-30 15:44
閱讀 1168·2019-08-29 12:12
閱讀 3263·2019-08-27 10:58