Spring Boot整合mybatis(一)实例代码
sprig-boot是一个微服务架构,加快了spring工程快速开发,以及简便了配置。接下来开始spring-boot与mybatis的整合。
1、创建一个maven工程命名为spring-boot-entity,pom.xml文件配置如下:
4.0.0 com.spring.boot.entity spring-boot-entity jar 0.0.1-SNAPSHOT 然后创建一个包,命名为com.spring.boot.entity,在该包下创建一个User.java文件,内容如下: /** *Copyright(c)Windliven2016AllRightsReserved * *@authorliyj *@date2017年7月11日下午2:34:32 *@sinceV1.0.0 */ packagecom.spring.boot.entity; /** *TODO * *@authorliyj *@date2017年7月11日下午2:34:32 * */ publicclassUserEntity{ /** *id */ privateStringid; /** *name */ privateStringname; /** *pass */ privateStringpass; /** *email */ privateStringemail; /** *iphone */ privateStringiphone; publicUserEntity(){} publicUserEntity(Stringid){this.id=id;} publicStringgetId(){ returnid; } publicvoidsetId(Stringid){ this.id=id; } publicStringgetName(){ returnname; } publicvoidsetName(Stringname){ this.name=name; } publicStringgetPass(){ returnpass; } publicvoidsetPass(Stringpass){ this.pass=pass; } publicStringgetEmail(){ returnemail; } publicvoidsetEmail(Stringemail){ this.email=email; } publicStringgetIphone(){ returniphone; } publicvoidsetIphone(Stringiphone){ this.iphone=iphone; } } releases NexusReleaseRepository http://localhost:8081/nexus/content/repositories/releases/ snapshots NexusSnapshotRepository http://localhost:8081/nexus/content/repositories/snapshots/
到此,spring-boot-entity工程创建完成。
2、创建一个maven工程,命名为spring-boot-interface,pom.xml文件配置如下:
4.0.0 com.spring.boot.inter spring-boot-interface 0.0.1-SNAPSHOT com.spring.boot.entity spring-boot-entity 0.0.1-SNAPSHOT 然后创建一个包,命名为com.spring.boot.inter.service,在该包下创建UserService.java接口类。内容如下: /** *Copyright(c)Windliven2016AllRightsReserved * *@authorliyj *@date2017年7月11日下午2:31:20 *@sinceV1.0.0 */ packagecom.spring.boot.inter.service; importjava.util.List; importcom.spring.boot.entity.UserEntity; /** *TODO * *@authorliyj *@date2017年7月11日下午2:31:20 * */ publicinterfaceUserService{ /* *insert */ voidinsert(UserEntityentity); /* *deleteEntity */ voiddeleteEntity(UserEntityentity); /* *deleteById */ voiddeleteById(Stringid); /* *updateEntity */ voidupdateEntity(UserEntityentity); /* *updateById */ voidupdateById(Stringid); /* *getOne */ UserEntitygetOne(Stringid); /* *getList */ List releases NexusReleaseRepository http://localhost:8081/nexus/content/repositories/releases/ snapshots NexusSnapshotRepository http://localhost:8081/nexus/content/repositories/snapshots/ getList(); }
到此,spring-boot-interface工程完成。
3、创建一个maven工程,命名为spring-boot-main,pom.xml文件内容如下:
4.0.0 com.spring.boot.service spring-boot-service 0.0.1-SNAPSHOT com.spring.boot.entity spring-boot-entity 0.0.1-SNAPSHOT com.spring.boot.inter spring-boot-interface 0.0.1-SNAPSHOT org.springframework.boot spring-boot 1.4.1.RELEASE org.springframework.boot spring-boot-test 1.4.1.RELEASE org.springframework.boot spring-boot-starter 1.4.1.RELEASE org.springframework.boot spring-boot-starter-web 1.4.1.RELEASE org.springframework spring-beans 4.3.1.RELEASE org.springframework spring-test 4.3.1.RELEASE org.mybatis.spring.boot mybatis-spring-boot-starter 1.1.1 org.mybatis mybatis-spring 1.2.2 org.mybatis mybatis 3.2.6 mysql mysql-connector-java 5.1.17 com.mchange c3p0 0.9.2.1 org.apache.commons commons-lang3 3.4 org.springframework.boot spring-boot-starter-cache 1.4.1.RELEASE net.sf.ehcache ehcache 2.9.0 redis.clients jedis 2.8.2 org.springframework.boot spring-boot-starter-redis 1.4.1.RELEASE org.springframework.boot spring-boot-maven-plugin 1.4.1.RELEASE releases NexusReleaseRepository http://localhost:8081/nexus/content/repositories/releases/ snapshots NexusSnapshotRepository http://localhost:8081/nexus/content/repositories/snapshots/
然后创建包,命名为com.spring.boot.base,在该包下创建DataBaseConfig.java文件,文件内容如下:
/** *Copyright(c)Windliven2016AllRightsReserved * *@authorliyj *@date2017年7月12日上午9:53:09 *@sinceV1.0.0 */ packagecom.spring.boot.base; importjavax.sql.DataSource; importorg.apache.ibatis.session.SqlSessionFactory; importorg.mybatis.spring.SqlSessionFactoryBean; importorg.mybatis.spring.mapper.MapperScannerConfigurer; importorg.slf4j.Logger; importorg.slf4j.LoggerFactory; importorg.springframework.context.annotation.Bean; importorg.springframework.context.annotation.Configuration; importorg.springframework.context.annotation.Primary; importorg.springframework.core.io.support.PathMatchingResourcePatternResolver; importorg.springframework.jdbc.datasource.DataSourceTransactionManager; importorg.springframework.transaction.PlatformTransactionManager; importorg.springframework.transaction.annotation.EnableTransactionManagement; importcom.mchange.v2.c3p0.ComboPooledDataSource; /** *TODO * *@authorliyj *@date2017年7月12日上午9:53:09 * */ @Configuration @EnableTransactionManagement publicclassDataBaseConfig{ privatefinalLoggerlog=LoggerFactory.getLogger(DataBaseConfig.class); @Bean @Primary publicDataSourcegetDataSource()throwsException{ log.debug("configdataSource"); ComboPooledDataSourcecpds=newComboPooledDataSource(); cpds.setDriverClass("com.mysql.jdbc.Driver"); cpds.setJdbcUrl("jdbc:mysql://localhost:3306/springboot"); cpds.setUser("root"); cpds.setPassword("root"); returncpds; } @Bean publicPlatformTransactionManagergetTransactionManager()throwsException{ returnnewDataSourceTransactionManager(getDataSource()); } @Bean publicSqlSessionFactorygetSqlSessionFactory()throwsException{ SqlSessionFactoryBeansfb=newSqlSessionFactoryBean(); sfb.setDataSource(getDataSource()); sfb.setTypeAliasesPackage("com.spring.boot.entity"); PathMatchingResourcePatternResolverresolver=newPathMatchingResourcePatternResolver(); sfb.setMapperLocations(resolver .getResources("classpath:/mapper/*.xml")); returnsfb.getObject(); } @Bean publicMapperScannerConfigurergetMapperScannerConfigurer(){ MapperScannerConfigurermsc=newMapperScannerConfigurer(); msc.setBasePackage("com.mybloc.personal.mapper"); msc.setSqlSessionFactoryBeanName("sqlSessionFactory"); returnmsc; } }
创建一个包,命名为com.spring.boot.dao,在该包下创建UserMapper.java文件,该文件是一个接口类,内容如下:
/** *Copyright(c)Windliven2016AllRightsReserved * *@authorliyj *@date2017年7月11日下午6:24:35 *@sinceV1.0.0 */ packagecom.spring.boot.dao; importjava.util.List; importcom.spring.boot.entity.UserEntity; /** *TODO * *@authorliyj *@date2017年7月11日下午6:24:35 * */ publicinterfaceUserMapper{ publicvoidinsertOne(UserEntityentity); publicvoiddelete(UserEntityentity); publicvoidupdate(UserEntityentity); publicUserEntitygetOne(UserEntityentity); publicListgetList(); }
在src/main/resources目录下创建一个mapper文件目录,在该目录下创建UserMapper.xml文件,内容如下:
insertintouser(id,name,pass,email,iphone) values(#{id},#{name},#{pass},#{email},#{iphone}) deletefromuserwhereid=#{id} updateusersetemail=#{email},iphone=#{iphone}whereid=#{id} selectid, name, pass, email, iphone fromuser whereid=#{id} selectid, name, pass, email, iphone fromuser
接下来创建包,命名为:com.spring.boot.service,在该包下创建UserServiceImpl.java文件,内容如下:
/** *Copyright(c)Windliven2016AllRightsReserved * *@authorliyj *@date2017年7月11日下午4:18:55 *@sinceV1.0.0 */ packagecom.spring.boot.service; importjava.util.List; importorg.springframework.beans.factory.annotation.Autowired; importorg.springframework.stereotype.Service; importorg.springframework.transaction.annotation.Transactional; importcom.spring.boot.dao.UserMapper; importcom.spring.boot.entity.UserEntity; importcom.spring.boot.inter.service.UserService; /** *TODO * *@authorliyj *@date2017年7月11日下午4:18:55 * */ @Transactional(readOnly=false) @Service publicclassUserServiceImplimplementsUserService{ @Autowired privateUserMapperuserMapper; /* *@seecom.spring.boot.inter.service.UserService#insert(com.spring.boot.entity.UserEntity) */ publicvoidinsert(UserEntityentity){ userMapper.insertOne(entity); System.out.println("insert"); } /* *@seecom.spring.boot.inter.service.UserService#deleteEntity(com.spring.boot.entity.UserEntity) */ publicvoiddeleteEntity(UserEntityentity){ userMapper.delete(entity); System.out.println("deleteEntity"); } /* *@seecom.spring.boot.inter.service.UserService#deleteById(java.lang.String) */ publicvoiddeleteById(Stringid){ userMapper.delete(newUserEntity(id)); System.out.println("deleteById"); } /* *@seecom.spring.boot.inter.service.UserService#updateEntity(com.spring.boot.entity.UserEntity) */ publicvoidupdateEntity(UserEntityentity){ userMapper.update(entity); System.out.println("updateEntity"); } /* *@seecom.spring.boot.inter.service.UserService#updateById(java.lang.String) */ publicvoidupdateById(Stringid){ userMapper.update(newUserEntity(id)); System.out.println("updateById"); } /* *@seecom.spring.boot.inter.service.UserService#getOne(java.lang.String) */ @Transactional(readOnly=true) publicUserEntitygetOne(Stringid){ returnuserMapper.getOne(newUserEntity(id)); } /* *@seecom.spring.boot.inter.service.UserService#getList() */ @Transactional(readOnly=true) publicListgetList(){ returnuserMapper.getList(); } }
再创建一个包,命名为com.spring.boot.controller,在该包下创建UserController.java文件,内容如下:
/** *Copyright(c)Windliven2016AllRightsReserved * *@authorliyj *@date2017年7月11日下午6:08:00 *@sinceV1.0.0 */ packagecom.spring.boot.controller; importjava.util.Collection; importorg.springframework.beans.factory.annotation.Autowired; importorg.springframework.beans.factory.annotation.Value; importorg.springframework.cache.Cache; importorg.springframework.cache.CacheManager; importorg.springframework.data.redis.core.RedisTemplate; importorg.springframework.data.redis.core.ValueOperations; importorg.springframework.web.bind.annotation.RequestMapping; importorg.springframework.web.bind.annotation.RequestMethod; importorg.springframework.web.bind.annotation.RequestParam; importorg.springframework.web.bind.annotation.RestController; importcom.spring.boot.entity.UserEntity; importcom.spring.boot.inter.service.UserService; /** *TODO * *@authorliyj *@date2017年7月11日下午6:08:00 * */ //@SpringBootApplication(scanBasePackages={"com.spring.boot"}) @RestController @RequestMapping("/user") publicclassUserController{ @Autowired privateUserServiceuserService; @Autowired privateCacheManagercacheManager; @Autowired privateRedisTemplateredisTemplate; @Value("${testName}") privateStringname; @Value("${testPass}") privateStringpass; @RequestMapping(value="/say",method=RequestMethod.GET) publicStringsayHello(){ return"hellospringboot"; } @RequestMapping(value="/insert",method=RequestMethod.GET) publicStringinsert(){ UserEntityuserEntity=newUserEntity(); userEntity.setId("111"); userEntity.setName("liyj"); userEntity.setPass("123456"); userEntity.setEmail("704603154@qq.com"); userEntity.setIphone("18211140412"); userService.insert(userEntity); return"success"; } @RequestMapping(value="/one/get",method=RequestMethod.GET) publicUserEntitygetOne(@RequestParamStringid){ returnuserService.getOne(id); } }
接着创建一个包,命名为com.spring.boot.start,在该包下创建StartMain.java文件,内容如下:
/** *Copyright(c)Windliven2016AllRightsReserved * *@authorliyj *@date2017年7月11日下午4:52:32 *@sinceV1.0.0 */ packagecom.spring.boot.start; importorg.mybatis.spring.annotation.MapperScan; importorg.springframework.boot.SpringApplication; importorg.springframework.boot.autoconfigure.EnableAutoConfiguration; importorg.springframework.context.annotation.ComponentScan; importorg.springframework.context.annotation.Configuration; /** *TODO * *@authorliyj *@date2017年7月11日下午4:52:32 * */ @EnableAutoConfiguration @ComponentScan(basePackages="com.spring.boot") @Configuration @MapperScan(value={"com.spring.boot.dao"}) publicclassStartMain{ publicstaticvoidmain(String[]args){ SpringApplication.run(StartMain.class,args); } }
最后测试,启动StartMain类中的main()方法,项目便启动了,可以正常的从浏览器中访问和测试。
总结
以上所述是小编给大家介绍的SpringBoot整合mybatis(一)实例代码,希望对大家有所帮助,如果大家有任何疑问请给我留言,小编会及时回复大家的。在此也非常感谢大家对毛票票网站的支持!