Springboot+Mybatis建立Maven项目

工具:IntelliJ IDEA 2017.2、Maven 3.3.9

一、建立简单项目

Springboot+Mybatis建立Maven项目_第1张图片Springboot+Mybatis建立Maven项目_第2张图片

 Springboot+Mybatis建立Maven项目_第3张图片Springboot+Mybatis建立Maven项目_第4张图片

Springboot+Mybatis建立Maven项目_第5张图片

二、配置pom文件

在此之前要确保Maven设置路径正确

File-Settings-Build-Build Tools-Maven中的Maven home directory设置maven的根目录

Springboot+Mybatis建立Maven项目_第6张图片

	
		UTF-8
		UTF-8
		1.8
		3.4.6
		1.3.2
		5.0.4
		1.1.9
	

	
		
			org.springframework.boot
			spring-boot-starter-web
		
		
			org.mybatis.spring.boot
			mybatis-spring-boot-starter
			1.3.2
		

		
			org.springframework.boot
			spring-boot-starter-test
			test
		

		
			com.alibaba
			druid
			${druid.version}
		

		
			mysql
			mysql-connector-java
			${mysql.version}
		

		
		
			org.projectlombok
			lombok
		
	

	
		
			
				org.springframework.boot
				spring-boot-maven-plugin
			
			
				org.mybatis.generator
				mybatis-generator-maven-plugin
				1.3.6
				
					
						tk.mybatis
						mapper
						3.3.8
					
					
						mysql
						mysql-connector-java
						${mysql.version}
					
				
			
		
	


三、项目结构

Springboot+Mybatis建立Maven项目_第7张图片

generatorConfig.xml配置文件





    
    
    
        
            
            
        

        
        

        
            
        

        
        
            
            
        

        
        
            
        

        
        
            
        

        
            
        

application.properties配置文件

#配置端口
server.port=8081

#连接数据库四要素
jdbc.driver=com.mysql.jdbc.Driver
jdbc.url=jdbc:mysql://localhost:3306/jeffy
jdbc.username=root
jdbc.password=111111

#mybtais相关
mapper_path=/mapper/*.xml
entity_package=com.example.jeffy.model

 Generator插件生成mapper接口,model对象,mapper.xml

Springboot+Mybatis建立Maven项目_第8张图片

四、Controller、Service

Controller

@RestController
@RequestMapping("/user")
public class UserController {

    @Autowired
    private UserService userService;

    @RequestMapping("/list")
    public List selectAll(){
        return userService.selectAll();
    }
}

ServiceImpl(Service接口自行建立)

@Service
public class UserServiceImpl implements UserService {

    @Autowired
    private UserInfoMapper userInfoMapper;

    @Override
    public List selectAll() {
        return userInfoMapper.selectAll();
    }
}

Mapper(mapper接口自行建立)

五、配置类

DataSourceConfig

@Configuration
@MapperScan("com.example.jeffy.mapper")
public class DataSourceConfig {

    /*获取配置数据库四要素*/
    @Value("${jdbc.url}")
    private String jdbcUrl;

    @Value("${jdbc.driver}")
    private String jdbcDriver;

    @Value("${jdbc.username}")
    private String jdbcUser;

    @Value("${jdbc.password}")
    private String jdbcPassword;

    @Bean(name="dataSource")
    public DruidDataSource createDataSource() throws Exception {
        DruidDataSource dataSource = new DruidDataSource();
        dataSource.setUrl(jdbcUrl);
        dataSource.setDriverClassName(jdbcDriver);
        dataSource.setUsername(jdbcUser);
        dataSource.setPassword(jdbcPassword);
        dataSource.setDefaultAutoCommit(false);
        return dataSource;
    }
}

SqlSessionFactoryConfig

@Configuration
@Slf4j
public class SessionFactoryConfig {

    @Autowired
    @Qualifier("dataSource")
    private DataSource dataSource;

    @Value("${mapper_path}")
    private String MAPPERPATH;

    @Value("${entity_package}")
    private String entityPackage;

    @Bean(name = "SqlSessionFactory")
    public SqlSessionFactoryBean createSqlSessionFactoryBean() throws Exception {
        SqlSessionFactoryBean sqlSessionFactoryBean = new SqlSessionFactoryBean();
        Resource[] resources = new PathMatchingResourcePatternResolver().getResources(MAPPERPATH);
        sqlSessionFactoryBean.setMapperLocations(resources);
        sqlSessionFactoryBean.setTypeAliasesPackage(entityPackage);
        sqlSessionFactoryBean.setDataSource(dataSource);
        return sqlSessionFactoryBean;
    }
}

六、进行测试

http://localhost:8081/user/list

Springboot+Mybatis建立Maven项目_第9张图片

ending 如有错误,欢迎指出,本人也只是渣渣一枚,就算简单的代码,也要手敲,切勿cv流~ 

你可能感兴趣的:(Java)