3 import com.mchange.v2.c3p0.ComboPooledDataSource;
4 import org.mybatis.spring.SqlSessionFactoryBean;
5 import org.mybatis.spring.mapper.MapperScannerConfigurer;
6 import org.springframework.beans.factory.annotation.Autowired;
7 import org.springframework.context.EnvironmentAware;
8 import org.springframework.context.annotation.Bean;
9 import org.springframework.context.annotation.Configuration;
10 import org.springframework.context.annotation.PropertySource;
11 import org.springframework.core.env.Environment;
12 import org.springframework.core.io.ClassPathResource;
13 import org.springframework.core.io.support.PathMatchingResourcePatternResolver;
14
15 import javax.sql.DataSource;
16 import java.beans.PropertyVetoException;
17 import java.io.IOException;
18
19 @Configuration
20 @PropertySource(value = “classpath:jdbc.properties”)//加载资源文件
21 public class SpringDaoConfig implements EnvironmentAware {//数据层
22
23 @Autowired
24 private Environment env;
25
26 @Override
27 public void setEnvironment(Environment environment) {
28 this.env = environment;
29 }
30
31 //配置数据源
32 @Bean
33 public DataSource dataSource() throws PropertyVetoException {
34 ComboPooledDataSource dataSource = new ComboPooledDataSource();
35 dataSource.setDriverClass(env.getProperty(“jdbc.driver”));
36 dataSource.setJdbcUrl(env.getProperty(“jdbc.url”));
37 dataSource.setUser(env.getProperty(“jdbc.user”));
38 dataSource.setPassword(env.getProperty(“jdbc.password”));
39 return dataSource;
40 }
41 //配置 mybatis
42 @Bean
43 public SqlSessionFactoryBean sqlSessionFactoryBean() throws IOException, PropertyVetoException {
44 SqlSessionFactoryBean factoryBean = new SqlSessionFactoryBean();
45 PathMatchingResourcePatternResolver resolver = new PathMatchingResourcePatternResolver();
46 factoryBean.setTypeAliasesPackage(“com.oukele.entity”);//实体类
47 factoryBean.setMapperLocations(resolver.getResources(“classpath:mapper/*.xml”));// 映射 sql文件
48 factoryBean.setConfigLocation(new ClassPathResource(“mybatis-config.xml”));//mybatis配置
49 factoryBean.setDataSource(dataSource());
50 return factoryBean;
51 }
52 // 简化调用 将 dao 放置 容器中 使用:比如
53 //@Autowired
54 //xxxMapper xxmaper ;
55 //也可以在类上面 直接使用 @ComponentScan 扫描接口
56 @Bean
57 public MapperScannerConfigurer mapperScannerConfigurer() {
58 MapperScannerConfigurer configurer = new MapperScannerConfigurer();
59 configurer.setBasePackage(“com.oukele.dao”);//扫描接口
60 configurer.setSqlSessionFactoryBeanName(“sqlSessionFactoryBean”);
61 return configurer;
62 }
63
64 }
SpringServlet 类 (扫描 )
1 package com.oukele.config.spring;
2
3 import org.springframework.context.annotation.ComponentScan;
4 import org.springframework.context.annotation.Configuration;
5
6 @Configuration
7 @ComponentScan(basePackages = “com.oukele.servlet”)
8 public class SpringServlet {//服务层
9
10 }
WebConfig 类 (类似于 web.xml文件)
1 package com.oukele.config.web;
2
3 import com.oukele.config.mvc.SpringWebConfig;
4 import com.oukele.config.spring.RootConfig;
5 import org.springframework.web.filter.CharacterEncodingFilter;
6 import org.springframework.web.servlet.support.AbstractAnnotationConfigDispatcherServletInitializer;
7
8 import javax.servlet.Filter;
9
10 public class WebConfig extends AbstractAnnotationConfigDispatcherServletInitializer {
11
12 @Override
13 protected Class<?>[] getRootConfigClasses() {// (spring容器) 父容器
14 return new Class[]{RootConfig.class};
15 }
16
17 @Override
18 protected Class<?>[] getServletConfigClasses() {// (spring mvc容器) 子容器
19 return new Class[]{SpringWebConfig.class};
20 }
21
22 @Override
23 protected String[] getServletMappings() {//映射
24 return new String[]{“/”};
25 }
26
27 //设置编码 这里设置好像没有用。。。。。 有解决方案请告诉我,谢谢
28 @Override
29 protected Filter[] getServletFilters() {
30 CharacterEncodingFilter characterEncodingFilter = new CharacterEncodingFilter();
31 characterEncodingFilter.setEncoding(“UTF-8”);
32 return new Filter[]{characterEncodingFilter};
33 }
34 }
Controller 包 里面的类
1 package com.oukele.controller;
2
3 import com.oukele.entity.User;
4 import com.oukele.servlet.UserServlet;
5 import org.springframework.beans.factory.annotation.Autowired;
6 import org.springframework.web.bind.annotation.*;
7 import org.springframework.web.context.WebApplicationContext;
8
9 import javax.servlet.http.HttpSession;
10 import java.util.HashMap;
11 import java.util.Objects;
12
13 @RestController
14 public class LoginController {
15
16 @Autowired
17 private WebApplicationContext webApplicationContext;
18
19 @Autowired
20 private UserServlet userServlet;
21
22 @RequestMapping(path = “/login/{name}/{password}”, method = RequestMethod.GET,produces = “application/json;charset=utf-8”)
23 public String login(@PathVariable(“name”) String name, @PathVariable(“password”) String password, HttpSession session) {
24 User user = new User();
25 user.setUserName(name);
26 user.setPassword(password);
27 session.setAttribute(“username”,user);//存入session中
28 Boolean aBoolean = userServlet.checkUser(user);
29
30 if(aBoolean){
31 return “{“msg”:“登入成功”}”;
32 }
33
34 return “{“msg”:“登入失败”}”;
35 }
36
37 @GetMapping(path = “/Ioc”)
38 public HashMap<String, String[]> getAllInfo() {
39
40 return new HashMap<String, String[]>() {{
41 put(“子容器”, webApplicationContext.getBeanDefinitionNames());
42 put(“父容器”, Objects.requireNonNull(webApplicationContext.getParent().getBeanDefinitionNames()));
43 }};
44
45 }
46
47 }
dao包中的接口
1 package com.oukele.dao;
2
3 import com.oukele.entity.User;
4 import org.apache.ibatis.annotations.Select;
5 import org.springframework.stereotype.Repository;
6
7 @Repository
8 public interface UserMapper {
9
10 //使用xml配置文件
11 int check(User user);
12 //使用注解
13 @Select(“select count(*) from user where userName = #{userName} and password = #{password}”)
14 int check1(User user);
15
16 }
entity包中的类
1 package com.oukele.entity;
2
3 public class User {
4 private String userName;
5 private String password;
6
7 public String getUserName() {
8 return userName;
9 }
10
11 public void setUserName(String userName) {
12 this.userName = userName;
13 }
14
15 public String getPassword() {
16 return password;
17 }
18
19 public void setPassword(String password) {
20 this.password = password;
21 }
22
23 @Override
24 public String toString() {
25 return “User{” +
26 “userName='” + userName + ‘’’ +
27 “, password='” + password + ‘’’ +
28 ‘}’;
29 }
30 }
servlet包中的类
1 package com.oukele.servlet;
2
3 import com.oukele.dao.UserMapper;
4 import com.oukele.entity.User;
5 import org.springframework.beans.factory.annotation.Autowired;
6 import org.springframework.stereotype.Service;
7
8 @Service
9 public class UserServlet {
10
11 @Autowired
12 private UserMapper userMapper;
13
14 public Boolean checkUser(User user){
15 //根据传进来的参数,去数据库查找用户
16 int result = userMapper.check(user);
17 //如果查回来的结果不为零,说明数据库中有该用户,让他登陆,否则不让他登陆
18 if( result > 0 ){
19 return true;
20 }
21 return false;