189 8069 5689

SpringBoot整合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


releases
Nexus Release Repository
http://localhost:8081/nexus/content/repositories/releases/


snapshots
Nexus Snapshot Repository
http://localhost:8081/nexus/content/repositories/snapshots/




然后创建一个包,命名为com.spring.boot.entity,在该包下创建一个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;
}
}

到此,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




releases
Nexus Release Repository
http://localhost:8081/nexus/content/repositories/releases/


snapshots
Nexus Snapshot Repository
http://localhost:8081/nexus/content/repositories/snapshots/




然后创建一个包,命名为com.spring.boot.inter.service,在该包下创建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 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
Nexus Release Repository
http://localhost:8081/nexus/content/repositories/releases/


snapshots
Nexus Snapshot Repository
http://localhost:8081/nexus/content/repositories/snapshots/


然后创建包,命名为com.spring.boot.base,在该包下创建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;
}
}

创建一个包,命名为com.spring.boot.dao,在该包下创建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 List getList();
}

在src/main/resources目录下创建一个mapper文件目录,在该目录下创建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}





接下来创建包,命名为:com.spring.boot.service,在该包下创建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 List getList() {
return userMapper.getList();
}
}

再创建一个包,命名为com.spring.boot.controller,在该包下创建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 RedisTemplate redisTemplate;
@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);
}
}

接着创建一个包,命名为com.spring.boot.start,在该包下创建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(一)实例代码,希望对大家有所帮助,如果大家有任何疑问请给我留言,小编会及时回复大家的。在此也非常感谢大家对创新互联网站的支持!


本文标题:SpringBoot整合mybatis(一)实例代码
当前地址:http://jkwzsj.com/article/jhdoij.html

其他资讯