SpringBoot配置Mybatis的三种方式

仅供参考:
*第一种:新建一个maven工程,配置pom和application.properties文件SpringBoot配置Mybatis的三种方式_第1张图片*springBootConfig:
SpringBoot配置Mybatis的三种方式_第2张图片
springConfig:
SpringBoot配置Mybatis的三种方式_第3张图片
springMvc:
SpringBoot配置Mybatis的三种方式_第4张图片

pom.xml:`

4.0.0



	org.springframework.boot
	spring-boot-starter-parent
	1.5.2.RELEASE

com.youzhong
ssm-boot
0.0.1-SNAPSHOT
war


	
	
		org.springframework.boot
		spring-boot-devtools
		true
	
	
		org.springframework.boot
		spring-boot-starter-web
	

	
	
		org.springframework.boot
		spring-boot-starter-tomcat
		provided
	
	
		org.apache.tomcat.embed
		tomcat-embed-jasper
		provided
	
	
	
		javax.servlet
		jstl
	
	
	
	    org.mybatis.spring.boot
	    mybatis-spring-boot-starter
	    1.3.0
	
	
	
	    mysql
	    mysql-connector-java
		


	
		
			org.springframework.boot
			spring-boot-maven-plugin
		
	

`
application.properties文件:

server.port=9090
server.context-path=/
#rizhi
logging.file=D:/mylog/log.log
logging.level.org.springframework.web = DEBUG
##
spring.mvc.view.prefix=/WEB-INF/jsp/
spring.mvc.view.suffix=.jsp
# \u6570\u636E\u6E90\u914D\u7F6E
spring.datasource.driver-class-name=com.mysql.jdbc.Driver
spring.datasource.url=jdbc:mysql://localhost:3306/spring?useUnicode=true&characterEncoding=utf8&serverTimezone=UTC
spring.datasource.username=root
spring.datasource.password=root
# mybatis \u914D\u7F6E
#mybatis.config-location=classpath:mybatis/mybatis-config.xml
mybatis.mapper-locations=classpath:/mapper/*.xml

第二种:
1 创建maven,搭建springboot环境
2 在pom.xml文件中引入依赖

    
        org.springframework.boot
        spring-boot-starter-jdbc
    
    
	
	
	    mysql
	    mysql-connector-java
	
	
	
    
        com.alibaba
        druid
        1.0.14
    
	 
    
        org.mybatis
        mybatis
        3.2.8
    
    
        org.mybatis
        mybatis-spring
        1.2.2
     

3 创建jdbc.properties
##for mysql
jdbc.driver=com.mysql.jdbc.Driver
#jdbc.url=jdbc:mysql://60.220.247.94:3306/czsctest?useUnicode=true&characterEncoding=UTF-8
jdbc.jdbcUrl=jdbc:mysql://localhost:3306/springboot?useUnicode=true&characterEncoding=UTF-8
#jdbc.url=jdbc:mysql://60.220.247.94:3306/czsctest
jdbc.username=root
jdbc.password=root

4 创建JdbcConfig.java类,通过@ConfigurationProperties读取jdbc.properties属性信息

//前缀 + 属性名称 === 属性文件的key

@Component
@ConfigurationProperties(prefix="jdbc")
public class JdbcConfig {
	
	private String driver;//jdbc.driver=
	private String jdbcUrl;//jdbc.jdbcUrl=
	private String username;//jdbc.username
	private String password;//jdbc.password		
	。。。。
	
	}

5 创建datasource 配置类
@Configuration
public class DataSourceConfig {
@Autowired
private JdbcConfig jdbcConfig;
@Bean(“dataSource”)
public DruidDataSource buildDataSource(){
DruidDataSource dataSource = new DruidDataSource();

	dataSource.setUrl(jdbcConfig.getJdbcUrl());
	dataSource.setUsername(jdbcConfig.getUsername());
	dataSource.setDriverClassName(jdbcConfig.getDriver());
	dataSource.setPassword(jdbcConfig.getPassword());
	return dataSource;
}

}

6 创建mybaits的配置类
@Configuration
@PropertySource(“classpath:jdbc.properties”)
public class MybaitisConfig {
@Autowired
private DataSource dataSource;
//spring与mybaits整合
//1 配置datasoruce
//2 创建meybiats的sqlsessionfacory
//3 扫描mybats的接口mapper接口
@Bean(“sqlSessionFactory”)
public SqlSessionFactory sqlSessionFactory(DataSource dataSource) throws Exception {

        SqlSessionFactoryBean sqlSessionFactoryBean = new SqlSessionFactoryBean();
        // 设置数据源
        sqlSessionFactoryBean.setDataSource(dataSource);

        // 设置mybatis的主配置文件

// sqlSessionFactoryBean.setConfigLocation(new ClassPathResource(“mybatis/mybatis-config.xml”));

        ResourcePatternResolver  resolver = new PathMatchingResourcePatternResolver();
        
        Resource[] resources = resolver.getResources("classpath:/mapper/*.xml");
        
        sqlSessionFactoryBean.setMapperLocations(resources);

        // 设置别名包
        sqlSessionFactoryBean.setTypeAliasesPackage("com.light.springboot.domain");

        return sqlSessionFactoryBean.getObject();
    }
@Bean
public  MapperScannerConfigurer  buildMapperScannerConfigurer(){
	
	MapperScannerConfigurer config = new MapperScannerConfigurer();
	
	//设置扫描的mapper的接口包
	config.setBasePackage("com.yzit.plateform.dao");
	
	config.setSqlSessionFactoryBeanName("sqlSessionFactory");
	
	return config;
}

}

7 创建数据库以及表
8 mybaits代码生成mapper接口以及mapper.xml

9 创建service、接口以及实现类

10 创建controller
//@RestController == @controller +@responbody
@RestController
public class RoleController {
@Autowired
private RoleService roleService;

@RequestMapping("/role/v_list.do")
public List findAll(){
	return roleService.findAll();	
}

}
11 创建springmvc的配置

@Configuration
@ComponentScan({“com.yzit.plateform.controller”,“com.yzit.plateform.service”})
@MapperScan(value=“com.yzit.plateform.dao”)
public class SpringConfig {

}
12 创建springboot的启动类
@SpringBootApplication
public class SpringBootConfig {
public static void main(String[] args) {
SpringApplication.run(SpringBootConfig.class, args);
}
}
第三种:

1 创建maven,搭建springboot环境

2 在pom.xml文件中引入依赖

org.springframework.boot spring-boot-starter-aop org.springframework.boot spring-boot-starter-jdbc
	
	
	    mysql
	    mysql-connector-java
	
	
	
    
        com.alibaba
        druid
        1.0.14
    
	 
    
        org.mybatis
        mybatis
        3.2.8
    
    
        org.mybatis
        mybatis-spring
        1.2.2
     

3 创建jdbc.properties
##for mysql
jdbc.driver=com.mysql.jdbc.Driver
#jdbc.url=jdbc:mysql://60.220.247.94:3306/czsctest?useUnicode=true&characterEncoding=UTF-8
jdbc.jdbcUrl=jdbc:mysql://localhost:3306/springboot?useUnicode=true&characterEncoding=UTF-8
#jdbc.url=jdbc:mysql://60.220.247.94:3306/czsctest
jdbc.username=root
jdbc.password=root
4 创建spring-mybagti的配置文件


	
		
			classpath:jdbc.properties
		
	




	
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	





	
	
	
	
	
	
	
		
			classpath*:/mapper/*Mapper.xml
		
	




	




	








	
		
		
		
		
		
		
	



	
	

5 在springconfig配置类使用importresource注解加载步骤4创建spring-mybaits配置文件
@Configuration
@ComponentScan({“com.yzit.plateform.controller”,“com.yzit.plateform.service”})
@MapperScan(value=“com.yzit.plateform.dao”)
@ImportResource(value = { “classpath:spring-context-mybaits.xml” })
public class SpringConfig {

}

你可能感兴趣的:(SpringBoot)