十年網(wǎng)站開發(fā)經驗 + 多家企業(yè)客戶 + 靠譜的建站團隊
量身定制 + 運營維護+專業(yè)推廣+無憂售后,網(wǎng)站問題一站解決
sprig-boot是一個微服務架構,加快了spring工程快速開發(fā),以及簡便了配置。接下來開始spring-boot與mybatis的整合。
創(chuàng)新互聯(lián)公司是一家集網(wǎng)站建設,岑鞏企業(yè)網(wǎng)站建設,岑鞏品牌網(wǎng)站建設,網(wǎng)站定制,岑鞏網(wǎng)站建設報價,網(wǎng)絡營銷,網(wǎng)絡優(yōu)化,岑鞏網(wǎng)站推廣為一體的創(chuàng)新建站企業(yè),幫助傳統(tǒng)企業(yè)提升企業(yè)形象加強企業(yè)競爭力??沙浞譂M足這一群體相比中小企業(yè)更為豐富、高端、多元的互聯(lián)網(wǎng)需求。同時我們時刻保持專業(yè)、時尚、前沿,時刻以成就客戶成長自我,堅持不斷學習、思考、沉淀、凈化自己,讓我們?yōu)楦嗟钠髽I(yè)打造出實用型網(wǎng)站。
1、創(chuàng)建一個maven工程命名為spring-boot-entity,pom.xml文件配置如下:
然后創(chuàng)建一個包,命名為com.spring.boot.entity,在該包下創(chuàng)建一個User.java文件,內容如下: /** * Copyright (c) Windliven 2016 All Rights Reserved * * @author liyj * @date 2017年7月11日 下午2:34:32 * @since V1.0.0 */ package com.spring.boot.entity; /** * TODO * * @author liyj * @date 2017年7月11日 下午2:34:32 * */ public class UserEntity { /** * id */ private String id; /** * name */ private String name; /** * pass */ private String pass; /** * email */ private String email; /** * iphone */ private String iphone; public UserEntity() {} public UserEntity(String id) {this.id = id;} public String getId() { return id; } public void setId(String id) { this.id = id; } public String getName() { return name; } public void setName(String name) { this.name = name; } public String getPass() { return pass; } public void setPass(String pass) { this.pass = pass; } public String getEmail() { return email; } public void setEmail(String email) { this.email = email; } public String getIphone() { return iphone; } public void setIphone(String iphone) { this.iphone = iphone; } } 4.0.0 com.spring.boot.entity spring-boot-entity jar 0.0.1-SNAPSHOT releases Nexus Release Repository http://localhost:8081/nexus/content/repositories/releases/ snapshots Nexus Snapshot Repository http://localhost:8081/nexus/content/repositories/snapshots/
到此,spring-boot-entity工程創(chuàng)建完成。
2、創(chuàng)建一個maven工程,命名為spring-boot-interface,pom.xml文件配置如下:
然后創(chuàng)建一個包,命名為com.spring.boot.inter.service,在該包下創(chuàng)建UserService.java接口類。內容如下: /** * Copyright (c) Windliven 2016 All Rights Reserved * * @author liyj * @date 2017年7月11日 下午2:31:20 * @since V1.0.0 */ package com.spring.boot.inter.service; import java.util.List; import com.spring.boot.entity.UserEntity; /** * TODO * * @author liyj * @date 2017年7月11日 下午2:31:20 * */ public interface UserService { /* * insert */ void insert(UserEntity entity); /* * deleteEntity */ void deleteEntity(UserEntity entity); /* * deleteById */ void deleteById(String id); /* * updateEntity */ void updateEntity(UserEntity entity); /* * updateById */ void updateById(String id); /* * getOne */ UserEntity getOne(String id); /* * getList */ List 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 releases Nexus Release Repository http://localhost:8081/nexus/content/repositories/releases/ snapshots Nexus Snapshot Repository http://localhost:8081/nexus/content/repositories/snapshots/ getList(); }
到此,spring-boot-interface工程完成。
3、創(chuàng)建一個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 Nexus Release Repository http://localhost:8081/nexus/content/repositories/releases/ snapshots Nexus Snapshot Repository http://localhost:8081/nexus/content/repositories/snapshots/
然后創(chuàng)建包,命名為com.spring.boot.base,在該包下創(chuàng)建DataBaseConfig.java文件,文件內容如下:
/** * Copyright (c) Windliven 2016 All Rights Reserved * * @author liyj * @date 2017年7月12日 上午9:53:09 * @since V1.0.0 */ package com.spring.boot.base; import javax.sql.DataSource; import org.apache.ibatis.session.SqlSessionFactory; import org.mybatis.spring.SqlSessionFactoryBean; import org.mybatis.spring.mapper.MapperScannerConfigurer; import org.slf4j.Logger; import org.slf4j.LoggerFactory; import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; import org.springframework.context.annotation.Primary; import org.springframework.core.io.support.PathMatchingResourcePatternResolver; import org.springframework.jdbc.datasource.DataSourceTransactionManager; import org.springframework.transaction.PlatformTransactionManager; import org.springframework.transaction.annotation.EnableTransactionManagement; import com.mchange.v2.c3p0.ComboPooledDataSource; /** * TODO * * @author liyj * @date 2017年7月12日 上午9:53:09 * */ @Configuration @EnableTransactionManagement public class DataBaseConfig { private final Logger log = LoggerFactory.getLogger(DataBaseConfig.class); @Bean @Primary public DataSource getDataSource() throws Exception { log.debug("config dataSource"); ComboPooledDataSource cpds = new ComboPooledDataSource(); cpds.setDriverClass("com.mysql.jdbc.Driver"); cpds.setJdbcUrl("jdbc:mysql://localhost:3306/springboot"); cpds.setUser("root"); cpds.setPassword("root"); return cpds; } @Bean public PlatformTransactionManager getTransactionManager() throws Exception { return new DataSourceTransactionManager(getDataSource()); } @Bean public SqlSessionFactory getSqlSessionFactory() throws Exception { SqlSessionFactoryBean sfb = new SqlSessionFactoryBean(); sfb.setDataSource(getDataSource()); sfb.setTypeAliasesPackage("com.spring.boot.entity"); PathMatchingResourcePatternResolver resolver = new PathMatchingResourcePatternResolver(); sfb.setMapperLocations(resolver .getResources("classpath:/mapper/*.xml")); return sfb.getObject(); } @Bean public MapperScannerConfigurer getMapperScannerConfigurer() { MapperScannerConfigurer msc = new MapperScannerConfigurer(); msc.setBasePackage("com.mybloc.personal.mapper"); msc.setSqlSessionFactoryBeanName("sqlSessionFactory"); return msc; } }
創(chuàng)建一個包,命名為com.spring.boot.dao,在該包下創(chuàng)建UserMapper.java文件,該文件是一個接口類,內容如下:
/** * Copyright (c) Windliven 2016 All Rights Reserved * * @author liyj * @date 2017年7月11日 下午6:24:35 * @since V1.0.0 */ package com.spring.boot.dao; import java.util.List; import com.spring.boot.entity.UserEntity; /** * TODO * * @author liyj * @date 2017年7月11日 下午6:24:35 * */ public interface UserMapper { public void insertOne(UserEntity entity); public void delete(UserEntity entity); public void update(UserEntity entity); public UserEntity getOne(UserEntity entity); public ListgetList(); }
在src/main/resources目錄下創(chuàng)建一個mapper文件目錄,在該目錄下創(chuàng)建UserMapper.xml文件,內容如下:
<?xml version="1.0" encoding="UTF-8" ?>insert into user(id,name,pass,email,iphone) values(#{id},#{name},#{pass},#{email},#{iphone}) delete from user where id = #{id} update user set email = #{email},iphone = #{iphone} where id = #{id}
接下來創(chuàng)建包,命名為:com.spring.boot.service,在該包下創(chuàng)建UserServiceImpl.java文件,內容如下:
/** * Copyright (c) Windliven 2016 All Rights Reserved * * @author liyj * @date 2017年7月11日 下午4:18:55 * @since V1.0.0 */ package com.spring.boot.service; import java.util.List; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Service; import org.springframework.transaction.annotation.Transactional; import com.spring.boot.dao.UserMapper; import com.spring.boot.entity.UserEntity; import com.spring.boot.inter.service.UserService; /** * TODO * * @author liyj * @date 2017年7月11日 下午4:18:55 * */ @Transactional(readOnly=false) @Service public class UserServiceImpl implements UserService { @Autowired private UserMapper userMapper; /* * @see com.spring.boot.inter.service.UserService#insert(com.spring.boot.entity.UserEntity) */ public void insert(UserEntity entity) { userMapper.insertOne(entity); System.out.println("insert"); } /* * @see com.spring.boot.inter.service.UserService#deleteEntity(com.spring.boot.entity.UserEntity) */ public void deleteEntity(UserEntity entity) { userMapper.delete(entity); System.out.println("deleteEntity"); } /* * @see com.spring.boot.inter.service.UserService#deleteById(java.lang.String) */ public void deleteById(String id) { userMapper.delete(new UserEntity(id)); System.out.println("deleteById"); } /* * @see com.spring.boot.inter.service.UserService#updateEntity(com.spring.boot.entity.UserEntity) */ public void updateEntity(UserEntity entity) { userMapper.update(entity); System.out.println("updateEntity"); } /* * @see com.spring.boot.inter.service.UserService#updateById(java.lang.String) */ public void updateById(String id) { userMapper.update(new UserEntity(id)); System.out.println("updateById"); } /* * @see com.spring.boot.inter.service.UserService#getOne(java.lang.String) */ @Transactional(readOnly=true) public UserEntity getOne(String id) { return userMapper.getOne(new UserEntity(id)); } /* * @see com.spring.boot.inter.service.UserService#getList() */ @Transactional(readOnly=true) public ListgetList() { return userMapper.getList(); } }
再創(chuàng)建一個包,命名為com.spring.boot.controller,在該包下創(chuàng)建UserController.java文件,內容如下:
/** * Copyright (c) Windliven 2016 All Rights Reserved * * @author liyj * @date 2017年7月11日 下午6:08:00 * @since V1.0.0 */ package com.spring.boot.controller; import java.util.Collection; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Value; import org.springframework.cache.Cache; import org.springframework.cache.CacheManager; import org.springframework.data.redis.core.RedisTemplate; import org.springframework.data.redis.core.ValueOperations; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RequestMethod; import org.springframework.web.bind.annotation.RequestParam; import org.springframework.web.bind.annotation.RestController; import com.spring.boot.entity.UserEntity; import com.spring.boot.inter.service.UserService; /** * TODO * * @author liyj * @date 2017年7月11日 下午6:08:00 * */ //@SpringBootApplication(scanBasePackages = {"com.spring.boot"}) @RestController @RequestMapping("/user") public class UserController { @Autowired private UserService userService; @Autowired private CacheManager cacheManager; @Autowired private RedisTemplateredisTemplate; @Value("${testName}") private String name; @Value("${testPass}") private String pass; @RequestMapping(value = "/say", method = RequestMethod.GET) public String sayHello() { return "hello spring boot"; } @RequestMapping(value = "/insert", method = RequestMethod.GET) public String insert() { UserEntity userEntity = new UserEntity(); 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) public UserEntity getOne(@RequestParam String id) { return userService.getOne(id); } }
接著創(chuàng)建一個包,命名為com.spring.boot.start,在該包下創(chuàng)建StartMain.java文件,內容如下:
/** * Copyright (c) Windliven 2016 All Rights Reserved * * @author liyj * @date 2017年7月11日 下午4:52:32 * @since V1.0.0 */ package com.spring.boot.start; import org.mybatis.spring.annotation.MapperScan; import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.EnableAutoConfiguration; import org.springframework.context.annotation.ComponentScan; import org.springframework.context.annotation.Configuration; /** * TODO * * @author liyj * @date 2017年7月11日 下午4:52:32 * */ @EnableAutoConfiguration @ComponentScan(basePackages = "com.spring.boot") @Configuration @MapperScan(value={"com.spring.boot.dao"}) public class StartMain { public static void main(String[] args) { SpringApplication.run(StartMain.class, args); } }
最后測試,啟動StartMain類中的main()方法,項目便啟動了,可以正常的從瀏覽器中訪問和測試。
總結
以上所述是小編給大家介紹的Spring Boot整合mybatis(一)實例代碼,希望對大家有所幫助,如果大家有任何疑問請給我留言,小編會及時回復大家的。在此也非常感謝大家對創(chuàng)新互聯(lián)網(wǎng)站的支持!