栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > Java

Spring Boot整合mybatis(一)实例代码

Java 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

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


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文件,内容如下:

package com.spring.boot.entity;

public class UserEntity {

private String id;

private String name;

private String pass;

private String email;

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接口类。内容如下:

package com.spring.boot.inter.service;
import java.util.List;
import com.spring.boot.entity.UserEntity;

public interface UserService {

void insert(UserEntity entity);

void deleteEntity(UserEntity entity);

void deleteById(String id);

void updateEntity(UserEntity entity);

void updateById(String id);

UserEntity getOne(String id);

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文件,文件内容如下:


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;

@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
package com.spring.boot.dao;
import java.util.List;
import com.spring.boot.entity.UserEntity;

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文件,内容如下:






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}



select id,
name,
pass,
email,
iphone
from user 

接下来创建包,命名为:com.spring.boot.service,在该包下创建UserServiceImpl.java文件,内容如下:


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;

@Transactional(readonly=false)
@Service
public class UserServiceImpl implements UserService {
@Autowired
private UserMapper userMapper;

public void insert(UserEntity entity) {
userMapper.insertOne(entity);
System.out.println("insert");
}

public void deleteEntity(UserEntity entity) {
userMapper.delete(entity);
System.out.println("deleteEntity");
}

public void deleteById(String id) {
userMapper.delete(new UserEntity(id));
System.out.println("deleteById");
}

public void updateEntity(UserEntity entity) {
userMapper.update(entity);
System.out.println("updateEntity");
}

public void updateById(String id) {
userMapper.update(new UserEntity(id));
System.out.println("updateById");
}

@Transactional(readonly=true)
public UserEntity getOne(String id) {
return userMapper.getOne(new UserEntity(id));
}

@Transactional(readonly=true)
public List getList() {
return userMapper.getList();
}
}

再创建一个包,命名为com.spring.boot.controller,在该包下创建UserController.java文件,内容如下:


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;

//@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文件,内容如下:


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;

@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(一)实例代码,希望对大家有所帮助,如果大家有任何疑问请给我留言,小编会及时回复大家的。在此也非常感谢大家对考高分网网站的支持!

转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/145222.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号