mysql数据库中的数据,想通过web的页面展示,有多种方式,可以通过python的django,java的ssm,现在接触java多些,这章就利用ssm框架搭建一个web
首先准备一个mysql数据库
接着准备ssm框架,包含四个domain、dao、service、web 模块进行搭建
domain准备javabean接收数据数据,需要保证mysql字段与javabean字段名一样
package com.hacker.ssm;
import java.util.Date;
public class Student {
private Integer id;
private String name;
private Integer age;
private Float high;
private String gender;
private Date birth;
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;
}
public Integer getAge() {
return age;
}
public void setAge(Integer age) {
this.age = age;
}
public Float getHigh() {
return high;
}
public void setHigh(Float high) {
this.high = high;
}
public String getGender() {
return gender;
}
public void setGender(String gender) {
this.gender = gender;
}
public Date getBirth() {
return birth;
}
public void setBirth(Date birth) {
this.birth = birth;
}
}
dao层就是准备mybatis要映射的接口
package com.hacker.ssm.dao;
import com.hacker.ssm.Student;
import org.apache.ibatis.annotations.Select;
import org.springframework.stereotype.Component;
import java.util.List;
@Component
public interface IStudentDao {
@Select("select * from students")
List<Student> findAll() throws Exception;
}
service层对数据进行处理操作
package com.hacker.ssm.service;
import com.hacker.ssm.Student;
import java.util.List;
public interface IStudentService {
List<Student> findAll() throws Exception;
}
package com.hacker.ssm.service.impl;
import com.hacker.ssm.Student;
import com.hacker.ssm.dao.IStudentDao;
import com.hacker.ssm.service.IStudentService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import java.util.List;
@Service
public class IStudentServiceImpl implements IStudentService {
@Autowired
private IStudentDao StudentDao;
public List<Student> findAll() throws Exception {
return StudentDao.findAll();
}
}
web层执行controller业务流程操作
package com.hacker.ssm.controller;
import com.hacker.ssm.Student;
import com.hacker.ssm.service.IStudentService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;
import java.util.List;
@Controller
@RequestMapping(path="/student")
public class StudentInfoController {
@Autowired
private IStudentService StudentService;
@RequestMapping(path="/findAll.do")
public ModelAndView finAll() throws Exception{
ModelAndView mv=new ModelAndView();
List<Student> all = StudentService.findAll();
for (int i = 0; i <all.size() ; i++) {
System.out.println(all.get(i).getName());
}
mv.addObject("studentinfo",all);
mv.setViewName("student");
return mv;
}
}
成功访问数据
http://localhost:8888/hacker_ssm_web1/student/findAll.do
源代码:https://github.com/porterOne/hacker_ssm_practice01