引入依赖

    
        
            junit
            junit
            4.11
            test
        
         
            org.springframework
            spring-webmvc
            5.3.19
        
        
            org.mybatis
            mybatis
            3.5.7
        
        
            mysql
            mysql-connector-java
            8.0.28
        
        
            org.mybatis
            mybatis-spring
            2.0.6
        
        
        
            org.springframework
            spring-jdbc
            5.1.2.RELEASE
        
        
        
            org.springframework
            spring-context
            5.1.2.RELEASE
        
        
        
            com.alibaba
            druid
            1.1.23
        
    

dataSource.properties配置

driverClass=com.mysql.cj.jdbc.Driver
url=jdbc:mysql://localhost:3306/test?user=root&password=admin&useUnicode=true
user=root
password=admin
这里有个大坑,我使用了druid连接池,要把username改成user不然报拒绝访问
  • 数据源配置

public class DataSourceConfig {
    //定义属性 为属性注入数据(数据的来源上面引入的db.properties文件)
    @Value("${driverClass}")
    private String driverClass;
    @Value("${url}")
    private String url;
    @Value("${user}")
    private String username;
    @Value("${password}")
    private String password;

    @Bean("dataSource")
    public DataSource dataSource(){
        DruidDataSource druidDataSource = new DruidDataSource();
        druidDataSource.setDriverClassName(driverClass);
        druidDataSource.setUrl(url);
        druidDataSource.setUsername(username);
        druidDataSource.setPassword(password);
        return druidDataSource;
    }

}
  • 配置mybatis
public class MybatisConfig {
    //创建SqlSessionFactoryBean对象,设置形参,Spring会自动去调用IOC容器中已有的数据源
    @Bean
    public SqlSessionFactoryBean sqlSessionFactoryBean(@Autowired DataSource dataSource){
        SqlSessionFactoryBean sqlSessionFactoryBean = new SqlSessionFactoryBean();
        sqlSessionFactoryBean.setDataSource(dataSource);
        return sqlSessionFactoryBean;
    }

//    @Bean
//    public MapperScannerConfigurer getMapperScannerConfigurer(){
//        MapperScannerConfigurer mp = new MapperScannerConfigurer();
//        mp.setBasePackage("com.lushannanlu.dao");
//        return mp;
//    }
}
  • spring整合mybatis核心配置
@Configuration
@ComponentScan("com.lushannanlu")//开启扫描
@Import({DataSourceConfig.class, MybatisConfig.class})//引入数据源配置和mybatis配置
@PropertySource("classpath:dataSource.properties")//引入数据源文件
@MapperScan("com.lushannanlu.dao")//开启dao层接口扫描
public class SpringConfig {
}

dao层

public interface UserDao {
    //查询所有用户
    @Select("select * from users")
    List queryUser();
}

public interface TestDao {
    @Select("select * from users where username = #{username}")
    User queryUserByName(@Param("username") String username);
}

serviceImp

@Service
public class UserServiceImp implements UserService{
    @Autowired
    private UserDao userDao;//接口代理对象
    @Autowired
    private TestDao testDao;//接口代理对象
    @Override
    public List queryUser() {
        return userDao.queryUser();
    }

    

	public User queryUserByName(String username){
	    return testDao.queryUserByName(username);
	}
}
  • 测试
 @Test
    public void shouldAnswerWithTrue()
    {
       ApplicationContext Context = new AnnotationConfigApplicationContext(SpringConfig.class);
        UserServiceImp userSericeImp = (UserServiceImp) Context.getBean("userServiceImp");
        User zhangsan = userSericeImp.queryUserByName("zhangsan");
        System.out.println(zhangsan);
    }

整合springmvc

1.删除web.xml
2.编写springmvc配置类
3.编写web.xml的配置类
@Configuration
@ComponentScan("com.lushannanlu.controller")
@EnableWebMvc
public class SpringMVCConfig implements WebMvcConfigurer {

    
    @Bean
    public InternalResourceViewResolver viewResolver(){
        InternalResourceViewResolver internalResourceViewResolver = new InternalResourceViewResolver();
        internalResourceViewResolver.setPrefix("/WEB-INF/jsp/");
        internalResourceViewResolver.setSuffix(".jsp");
        return internalResourceViewResolver;
    }
    
    @Override
    public void addResourceHandlers(ResourceHandlerRegistry registry) {
        registry.addResourceHandler("/pages
    @Override
    protected Class<?>[] getRootConfigClasses() {
        return new Class[]{SpringConfig.class};
    }

    
    @Override
    protected Class<?>[] getServletConfigClasses() {
        return new Class[]{SpringMVCConfig.class};
    }

    
    @Override
    protected String[] getServletMappings() {
        return new String[]{"/"};
    }
    
    @Override
    protected Filter[] getServletFilters() {
        CharacterEncodingFilter encodingFilter = new CharacterEncodingFilter();
        encodingFilter.setEncoding("utf-8");
        return new Filter[]{encodingFilter};
    }
}

1.《ssm整合基于纯注解》援引自互联网,旨在传递更多网络信息知识,仅代表作者本人观点,与本网站无关,侵删请联系页脚下方联系方式。

2.《ssm整合基于纯注解》仅供读者参考,本网站未对该内容进行证实,对其原创性、真实性、完整性、及时性不作任何保证。

3.文章转载时请保留本站内容来源地址,https://www.lu-xu.com/jiaoyu/2371899.html