资讯专栏INFORMATION COLUMN

redis-mongo项目介绍

cc17 / 2548人阅读

摘要:项目主要是在框架下对双数据源进行配置和操作的实例,关于框架搭建这里就不予展示与介绍了,想了解的请参考之前的项目介绍项目代码获取功能分类增删改查双数据源切换对类型和类型进行缓存插入与读取项目结构代码双数据源配置相关类数据库交互层实体对象层相

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

文章版权归作者所有,未经允许请勿转载,若此文章存在违规行为,您可以联系管理员删除。

转载请注明本文地址:https://www.ucloud.cn/yun/19095.html

相关文章

  • redis-mongo项目介绍

    摘要:项目主要是在框架下对双数据源进行配置和操作的实例,关于框架搭建这里就不予展示与介绍了,想了解的请参考之前的项目介绍项目代码获取功能分类增删改查双数据源切换对类型和类型进行缓存插入与读取项目结构代码双数据源配置相关类数据库交互层实体对象层相 redis-mongo 项目主要是在spring、springmvc、mybatis框架下对mysql、mysql双数据源、redis、mongo进...

    sixgo 评论0 收藏0
  • 一个JAVA渣渣的校招成长记,附BAT美团网易等20家面经总结

    摘要:作者重庆森林链接来源牛客网整个三月份通过牛客网和网友分享的经验学到了很多东西,现在反馈一下我的面试经历,希望对同学们有帮助。个人情况大三本方向渣硕,经过实验室学长内推,于三月底完成面试。校招是实力和运气的结合,缺一不可。 欢迎关注我的微信公众号:Java面试通关手册(坚持原创,分享美文,分享各种Java学习资源,面试题,以及企业级Java实战项目回复关键字免费领取):showImg(h...

    mozillazg 评论0 收藏0

发表评论

0条评论

最新活动
阅读需要支付1元查看
<