栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > Java

SSM整合进阶版

Java 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

SSM整合进阶版

        

目录

1.整体结构及呈现样式

2.添加依赖

3.自动生成代码配置(generator)

4.java部分代码展示

4.1 controller层下对应代码

4.1.1 StudentController

4.1.2 UserController

4.2 dao包

4.2.1 StudentMapper

4.2.2 UserMapper

4.3 entity包

4.3.1 Student

4.3.2 User

4.4 interceptor

4.4.1 LoginInterceptor

4.5 service

4.5.1 UserService

4.5.2 StudentService

4.5.3(1) UserServiceImpl

4.5.3(2) StudentServiceImpl

4.6 utils公共类

4.6.1 CommonResult

4.7 test(为自动生成)

5.resources部分

5.1 spring.xml

5.2 mapper包下

5.2.1 UserMapper.xml

5.2.2 StudentMapper.xml

6.webapp部分

6.1 WEB-INF

6.1.1 web.xml

6.2 前端界面代码

6.2.1 登录界面(login.jsp)

6.2.2 登录成功进入界面(elementui.html)


相对于之前的整合增加了登录界面及非登录者拦截,以及分页的使用、模糊查询的使用。

1.整体结构及呈现样式

2.添加依赖


    
      junit
      junit
      4.13.2
    

    
      org.springframework
      spring-webmvc
      5.2.15.RELEASE
    

    
      log4j
      log4j
      1.2.17
    

    
      org.slf4j
      slf4j-log4j12
      1.7.29
    

    
      org.projectlombok
      lombok
      1.18.24
    

    
      mysql
      mysql-connector-java
      8.0.28
    

    
      org.mybatis
      mybatis
      3.5.9
    

    
      javax.servlet
      javax.servlet-api
      4.0.1
    

    
      com.alibaba
      druid
      1.2.8
    

    
      com.fasterxml.jackson.core
      jackson-databind
      2.13.3
    

    
      org.mybatis
      mybatis-spring
      2.0.7
    

    
      org.springframework
      spring-jdbc
      5.2.15.RELEASE
    

    
      org.mybatis.generator
      mybatis-generator-core
      1.4.0
    

    
      com.fasterxml.jackson.datatype
      jackson-datatype-jsr310
      2.13.2
    

    
      com.github.pagehelper
      pagehelper
      5.3.0
    

  

3.自动生成代码配置(generator)

        只能自动生成student的相关代码。





    
    
    
    
        
        
            
        
        
        

        
            
        
        
        
            
            
        
        
        
            
        
        
        
            
        
        
        

4.java部分代码展示

4.1 controller层下对应代码

4.1.1 StudentController
@RestController
@Slf4j
public class StudentController {

    @Autowired
    private StudentService service;
    @RequestMapping(value = "/student",method = RequestMethod.POST,params = {"id"})
    public CommonResult getById(int id){
        return service.selectByPrimaryKey(id);
    }
    @RequestMapping(value = "/findStudent",method = RequestMethod.POST)
    public PageInfo findTestAll(Integer currentPage, Integer pageSize,@RequestBody Student student){
        log.info(currentPage+"================="+pageSize);
        return service.findAll(currentPage,pageSize,student);
    }
    @RequestMapping(value = "/deleteStudent",method = RequestMethod.POST)
    public CommonResult deleteTestAll(int id){
        return service.deleteByPrimaryKey(id);
    }
    //@RequestBody将json转换为java数据映射到student实体类中
    @RequestMapping(value = "/insertStudent",method = RequestMethod.POST)
    public CommonResult insertTestAll(@RequestBody Student student){
        return service.insert(student);
    }
    @RequestMapping(value = "/updateStudent",method = RequestMethod.POST)
    public CommonResult updateTestAll(@RequestBody Student student){
        System.out.println(student);
        return service.updateByPrimaryKeySelective(student);
    }
}

4.1.2 UserController
@RestController
public class UserController {
    @Autowired
    private UserService userService;
    @RequestMapping("/login")
    public CommonResult testLogin(String username, String password, HttpSession httpSession){
        return userService.getName(username, password,httpSession);
    }
}

4.2 dao包

4.2.1 StudentMapper
public interface StudentMapper {
    int deleteByPrimaryKey(Integer id);

    int insert(@Param("record") Student record, @Param("year") int year);

    int insertSelective(Student record);

    Student selectByPrimaryKey(Integer id);

    int updateByPrimaryKeySelective(@Param("record") Student record, @Param("birth") int year);

    int updateByPrimaryKey(Student record);

    List findAll(Student student);

}

4.2.2 UserMapper
public interface UserMapper {
    public User getName(@Param("name") String username, @Param("pass") String password, HttpSession httpSession);
}

4.3 entity包

4.3.1 Student
@AllArgsConstructor
@NoArgsConstructor
public class Student {
    private Integer id;

    private String name;

    private String sex;

    @JsonSerialize(using = LocalDateSerializer.class)
    @JsonDeserialize(using = LocalDateDeserializer.class)
    @JsonFormat(pattern = "yyyy-MM-dd", timezone = "GMT+8")
    @DateTimeFormat(pattern = "yyyy-MM-dd")
    private LocalDate birth;

    private String department;

    private String address;

    public Integer getId() {
        return id;
    }

    public void setId(Integer id) {
        this.id = id;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name == null ? null : name.trim();
    }

    public String getSex() {
        return sex;
    }

    public void setSex(String sex) {
        this.sex = sex == null ? null : sex.trim();
    }

    public LocalDate getBirth() {
        return birth;
    }

    public void setBirth(LocalDate birth) {
        this.birth = birth;
    }

    public String getDepartment() {
        return department;
    }

    public void setDepartment(String department) {
        this.department = department == null ? null : department.trim();
    }

    public String getAddress() {
        return address;
    }

    public void setAddress(String address) {
        this.address = address == null ? null : address.trim();
    }

    @Override
    public String toString() {
        return "Student{" +
                "id=" + id +
                ", name='" + name + ''' +
                ", sex='" + sex + ''' +
                ", birth='" + birth + ''' +
                ", department='" + department + ''' +
                ", address='" + address + ''' +
                '}';
    }
}

4.3.2 User
@Data
@AllArgsConstructor
@NoArgsConstructor
public class User {
    private int id;
    private String uername;
    private String password;

    public User(String uername, String password) {
        this.uername = uername;
        this.password = password;
    }
}

4.4 interceptor

4.4.1 LoginInterceptor
public class LoginInterceptor implements HandlerInterceptor {
    public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
        HttpSession session = request.getSession();
        Object user = session.getAttribute("user");
        if (user != null) {
            return true;
        }

        response.sendRedirect("login.jsp");
//        PrintWriter writer = response.getWriter();
//        response.setCharacterEncoding("utf-8");
//        response.setContentType("application/json;charset=utf-8");
//        writer.write(new ObjectMapper().writeValueAsString(CommonResult.error("未知异常")));
//        writer.flush();
//        writer.close();
        return false;
    }
}

4.5 service

4.5.1 UserService
public interface UserService {
    CommonResult getName(@Param("name") String username, @Param("pass") String password, HttpSession httpSession);
}

4.5.2 StudentService
public interface StudentService {
    CommonResult selectByPrimaryKey(Integer id);
    PageInfo findAll(Integer currentPage, Integer pageSize,Student student);
    CommonResult deleteByPrimaryKey(Integer id);
    CommonResult insert(Student record);
    CommonResult updateByPrimaryKeySelective(Student record);

}

4.5.3(1) UserServiceImpl
@Service
public class UserServiceImpl implements UserService {
    @Autowired
    private UserMapper userMapper;
    public CommonResult getName(String username, String password, HttpSession httpSession) {
        User passName = userMapper.getName(username, password, httpSession);
        if(passName!=null){
            httpSession.setAttribute("user",passName);
            return CommonResult.success(passName);
        }else {
            return CommonResult.notFont();
        }
    }
}

4.5.3(2) StudentServiceImpl
@Service
public class StudentServiceImpl implements StudentService {

    //byType
    @Autowired
    private StudentMapper studentMapper;

    public CommonResult selectByPrimaryKey(Integer id) {
        Student student = studentMapper.selectByPrimaryKey(id);
        return CommonResult.success(student);
    }

    public PageInfo findAll(Integer currentPage, Integer pageSize,Student student){
        PageHelper.startPage(currentPage,pageSize);
        List all = studentMapper.findAll(student);
        PageInfo studentPageInfo = new PageInfo<>(all);
        return studentPageInfo;
    }

    public CommonResult deleteByPrimaryKey(Integer id) {
        int i = studentMapper.deleteByPrimaryKey(id);
        return CommonResult.success(i);
    }

    public CommonResult insert(Student record) {
        int insert = studentMapper.insert(record,record.getBirth().getYear());

        return CommonResult.success(insert);
    }

    public CommonResult updateByPrimaryKeySelective(Student record) {
        int i = studentMapper.updateByPrimaryKeySelective(record,record.getBirth().getYear());
        return CommonResult.success(i);
    }


}

4.6 utils公共类

4.6.1 CommonResult
@Data
@AllArgsConstructor
@NoArgsConstructor
public class CommonResult {
    private final static int success=200;
    private final static int error=500;
    private final static int not_fount=404;

    private int code;
    private String msg;
    private Object data;

    public static CommonResult success(Object data){
        return new CommonResult(CommonResult.success,"查询成功",data);
    }
    public static CommonResult error(Object data){
        return new CommonResult(CommonResult.error,"登录异常,请联系管理员",data);
    }
    public static CommonResult notFont(){
        return new CommonResult(CommonResult.not_fount,"未找到",null);
    }
}

4.7 test(为自动生成)
public class UserTest {
    public static void main(String[] args) throws Exception {
        List warnings = new ArrayList();
        boolean overwrite = true;
        //注意此处的generator.xml,要跟自己的generator的全局配置文件命名一致generator在根目录下
        File configFile = new File("generator.xml");
        ConfigurationParser cp = new ConfigurationParser(warnings);
        Configuration config = cp.parseConfiguration(configFile);
        DefaultShellCallback callback = new DefaultShellCallback(overwrite);
        MyBatisGenerator myBatisGenerator = new MyBatisGenerator(config, callback, warnings);
        myBatisGenerator.generate(null);
    }
}

5.resources部分

5.1 spring.xml


        
    
    
    
    

    
    
        
        
        
        
        
        
        
        
        
        
        
        
        
     

    
    
        
        
        
        
        
        
            
                
                
            
        
    

    
    
        
        
    

    
    
        
            
            
            
            
            
            
        
    

5.2 mapper包下

5.2.1 UserMapper.xml


    select 
    
    from student2
    where id = #{id,jdbcType=INTEGER}