redis-mongo項目介紹

redis-mongo

項目主要是在spring、springmvc、mybatis框架下對mysql、mysql雙數據源、redis、mongo進行配置和操做的實例,關於框架搭建這裏就不予展現與介紹了,想了解的請參考以前的shiro項目介紹
項目代碼獲取:https://github.com/pysasuke/r...java

功能分類

  • mysql增刪改查mysql

  • mysql雙數據源切換git

  • redis對String類型和Object類型進行緩存github

  • mongo插入與讀取web

項目結構

java:代碼

  • core:雙數據源配置相關類redis

  • dao:數據庫交互層spring

  • entity:實體對象層sql

  • util:redis相關工具類數據庫

resources:配置文件

  • mapping:mybatis與實體映射文件存放包spring-mvc

  • 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

相關文章
相關標籤/搜索