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; 22 } 23 24 }
resources 目录
jdbc.properties 资源文件
1 jdbc.driver=org.mariadb.jdbc.Driver
2 jdbc.url=jdbc:mariadb://localhost:3306/test
3 jdbc.user=oukele
4 jdbc.password=oukele
mybatis-config.xml 配置文件
<?xml version="1.0" encoding="UTF-8" ?>
mapper文件中的 UserMapper.xml 配置文件
1 <?xml version="1.0" encoding="UTF-8"?>