本人小白没做过什么项目,因为要做springboot的毕设所以才开始研究,结果环境搭了2天都没整出来
最近一直报 Field userMapper in cn.net.nit.springboot.serviceImpl.UserServiceImpl required a bean of type 'cn.net.nit.springboot.mapper.UserMapper' that could not be found. 非常无奈
分层
MyBatisConfig
/*package cn.net.nit.springboot.javaconfig;
import javax.sql.DataSource;
import org.mybatis.spring.SqlSessionFactoryBean;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.core.io.Resource;
import org.springframework.core.io.support.PathMatchingResourcePatternResolver;
import org.springframework.core.io.support.ResourcePatternResolver;
@Configuration
public class MyBatisConfig {
@Autowired
private DataSource dataSource;
@Bean
@ConditionalOnMissingBean
public SqlSessionFactoryBean sqlSessionFactoryBean() {
SqlSessionFactoryBean sqlSessionFactoryBean=new SqlSessionFactoryBean();
sqlSessionFactoryBean.setDataSource(dataSource);
ResourcePatternResolver resolver=new PathMatchingResourcePatternResolver();
Resource mybatisConfigXml=resolver.getResource("classpath:mybatis/SqlMapConfig.xml");
sqlSessionFactoryBean.setConfigLocation(mybatisConfigXml);
//sqlSessionFactoryBean.setTypeAliasesPackage("cn.net.nit.po");
return sqlSessionFactoryBean;
}
}*/
package cn.net.nit.springboot.javaconfig;
import javax.sql.DataSource;
import org.mybatis.spring.SqlSessionFactoryBean;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean;
import org.springframework.boot.builder.SpringApplicationBuilder;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.PropertySource;
import org.springframework.core.io.Resource;
import org.springframework.core.io.support.PathMatchingResourcePatternResolver;
import org.springframework.core.io.support.ResourcePatternResolver;
import com.jolbox.bonecp.BoneCPDataSource;
/**
* Created by wxl on 2017/12/13.
* 数据源配置
*/
@Configuration //开启配置
@PropertySource(value = { "classpath:jdbc.properties" }, ignoreResourceNotFound = true) //引入文件 读取配置
@SpringBootApplication
public class MyBatisConfig {
@Value("${jdbc.url}")
private String jdbcUrl;
@Value("${jdbc.driverClassName}")
private String jdbcDriverClassName;
@Value("${jdbc.username}")
private String jdbcUsername;
@Value("${jdbc.password}")
private String jdbcPassword;
@Bean(destroyMethod = "close")
public DataSource dataSource() {
BoneCPDataSource boneCPDataSource = new BoneCPDataSource();
// 数据库驱动
boneCPDataSource.setDriverClass(jdbcDriverClassName);
// 相应驱动的jdbcUrl
boneCPDataSource.setJdbcUrl(jdbcUrl);
// 数据库的用户名
boneCPDataSource.setUsername(jdbcUsername);
// 数据库的密码
boneCPDataSource.setPassword(jdbcUsername);
// 检查数据库连接池中空闲连接的间隔时间,单位是分,默认值:240,如果要取消则设置为0
boneCPDataSource.setIdleConnectionTestPeriodInMinutes(60);
// 连接池中未使用的链接最大存活时间,单位是分,默认值:60,如果要永远存活设置为0
boneCPDataSource.setIdleMaxAgeInMinutes(30);
// 每个分区最大的连接数
boneCPDataSource.setMaxConnectionsPerPartition(100);
// 每个分区最小的连接数
boneCPDataSource.setMinConnectionsPerPartition(5);
return boneCPDataSource;
}
protected SpringApplicationBuilder configure(SpringApplicationBuilder builder) {
return builder.sources(MyBatisConfig.class);
}
@Bean
@ConditionalOnMissingBean //当容器里没有指定的Bean的情况下创建该对象
public SqlSessionFactoryBean sqlSessionFactoryBean(DataSource dataSource) {
SqlSessionFactoryBean sqlSessionFactoryBean = new SqlSessionFactoryBean();
// 设置数据源
sqlSessionFactoryBean.setDataSource(dataSource);
// 设置mybatis的主配置文件
ResourcePatternResolver resolver = new PathMatchingResourcePatternResolver();
Resource mybatisConfigXml = resolver.getResource("classpath:mybatis/SqlMapConfig.xml");
sqlSessionFactoryBean.setConfigLocation(mybatisConfigXml);
// 设置别名包
sqlSessionFactoryBean.setTypeAliasesPackage("cn.net.nit.springboot.po");
return sqlSessionFactoryBean;
}
}
MapperScannerConfig
package cn.net.nit.springboot.javaconfig;
import org.mybatis.spring.mapper.MapperScannerConfigurer;
import org.springframework.boot.autoconfigure.AutoConfigureAfter;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
@Configuration
@AutoConfigureAfter(MyBatisConfig.class) //保证在MyBatisConfig实例化之后再实例化该类
public class MapperScannerConfig {
@Bean
public MapperScannerConfigurer mapperScannerConfigurer() {
MapperScannerConfigurer mapperScannerConfigurer = new MapperScannerConfigurer();
mapperScannerConfigurer.setBasePackage("cn.net.nit.springboot.mapper");
return mapperScannerConfigurer;
}
}
UserMapper.java
UserMapper.xml
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
"http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<!-- 为这个mapper指定一个唯一的namespace,namespace的值习惯上设置成包名+sql映射文件名,这样就能够保证namespace的值是唯一的 -->
<mapper namespace="cn.net.nit.springboot.mapper.UserMapper">
<!-- 查询所有的用户 -->
<select id="getUserList" resultType="cn.net.nit.springboot.po.User">
select * from user;
</select>
</mapper>
UserServiceImpl
package cn.net.nit.springboot.serviceImpl;
import java.util.List;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import cn.net.nit.springboot.mapper.UserMapper;
import cn.net.nit.springboot.po.User;
import cn.net.nit.springboot.service.UserService;
@Service
public class UserServiceImpl implements UserService{
@Autowired
private UserMapper userMapper;
@Override
public List<User> getUserList() {
return userMapper.getUserList();
}
}
myspringboot
package cn.net.nit.springboot.first;
import java.util.List;
import org.mybatis.spring.annotation.MapperScan;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.*;
import org.springframework.boot.autoconfigure.*;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.stereotype.*;
import org.springframework.web.bind.annotation.*;
import cn.net.nit.springboot.po.User;
import cn.net.nit.springboot.service.UserService;
@Controller
@EnableAutoConfiguration
@ComponentScan(basePackages = "cn.net.nit.springboot.serviceImpl")
public class myspringboot {
/*@RequestMapping("/")
@ResponseBody
String home() {
return "My First Spring Boot!";
}*/
@Autowired
private UserService userService;
@ResponseBody
@RequestMapping("/userlist")
public List<User> getUserList(){
return userService.getUserList();
}
public static void main(String[] args) throws Exception {
SpringApplication.run(myspringboot.class, args);
}
}