这是我一年前做的项目的,算是SSM框架入门的实例,maven管理工具所用到的仓库私服为阿里云的仓库。
jdbc.properties是对数据库信息的配置:
#mysql version database setting
jdbc.driver=com.mysql.jdbc.Driver
jdbc.username=root
jdbc.password=
jdbc.url=jdbc\:mysql\://127.0.0.1\:3306/test?useUnicode=true&characterEncoding=UTF-8
service-context.xml:
Spring-database配置
classpath*:jdbc.properties
servlet-context.xml:
Spring-web MVC配置
5
zh_CN
mybatis-config.xml:
StudentMapper.xml,SQL语句编写的地方:
id,name,chinese,math,english
INSERT INTO student(name,chinese,math,english) VALUES (#{name}, #{chinese},#{math},#{english});
update student
name = #{name},
chinese =#{chinese},
math =#{math},
english=#{english}
where id = #{id}
delete from student where id = #{id}
MyBatis的Dao基类:
package com.HelloWorld.orm.mybatis;
import java.io.Serializable;
import java.util.List;
import org.mybatis.spring.support.SqlSessionDaoSupport;
/**
* MyBatis的Dao基类
* @author
*/
public class MyBatisDao extends SqlSessionDaoSupport{
public void save(String key, Object object) {
getSqlSession().insert(key, object);
}
public void delete(String key, Serializable id) {
getSqlSession().delete(key, id);
}
public void delete(String key, Object object) {
getSqlSession().delete(key, object);
}
public T get(String key, Object params) {
return (T) getSqlSession().selectOne(key, params);
}
public List getList(String key) {
return getSqlSession().selectList(key);
}
public List getList(String key, Object params) {
return getSqlSession().selectList(key, params);
}
}
entity实体类:
package com.HelloWorld.examples.base.entity;
public class Student {
private Integer id;
private String name;
private float chinese;
private float math;
private float english;
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 float getChinese() {
return chinese;
}
public void setChinese(float chinese) {
this.chinese = chinese;
}
public float getMath() {
return math;
}
public void setMath(float math) {
this.math = math;
}
public float getEnglish() {
return english;
}
public void setEnglish(float english) {
this.english = english;
}
}
Service层
package com.HelloWorld.examples.base.service;
import java.io.Serializable;
import java.util.List;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.HelloWorld.examples.base.entity.Student;
import com.HelloWorld.orm.mybatis.MyBatisDao;
@Service
public class StudentService {
@Autowired
private MyBatisDao myBatisDao;
public List getStudent(){
return myBatisDao.getList("StudentMapper.selectByEntity");
}
public Student getStudent(Serializable id){
return myBatisDao.get("StudentMapper.selectByPrimaryKey",id);
}
public void save(Student student){
if(student.getId()==null)
myBatisDao.save("StudentMapper.insert", student);
else
myBatisDao.save("StudentMapper.update",student);
}
public void delete(Serializable id){
myBatisDao.delete("StudentMapper.delete", id);
}
}
controller层:
package com.HelloWorld.examples.base.controller;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import com.HelloWorld.examples.base.entity.Student;
import com.HelloWorld.examples.base.service.StudentService;
@Controller
@RequestMapping("/student")
public class StudentController {
@Autowired
private StudentService studentService;
@RequestMapping(method = RequestMethod.GET)
public String list(Model model){
model.addAttribute("list", studentService.getStudent());
return "base/student_list";
}
@RequestMapping(value="/new", method = RequestMethod.GET)
public String create(Model model, Student student){
model.addAttribute("entity",student);
return "base/student_new";
}
@RequestMapping(value="/edit/{id}", method = RequestMethod.GET)
public String edit(Model model, @PathVariable Integer id){
model.addAttribute("entity", studentService.getStudent(id));
return "base/student_edit";
}
@RequestMapping(value="/save", method = RequestMethod.POST)
public String save(Model model, Student student){
studentService.save(student);
return "redirect:/student";
}
@RequestMapping(value="/view/{id}", method = RequestMethod.GET)
public String view(Model model, @PathVariable Integer id){
model.addAttribute("entity", studentService.getStudent(id));
return "base/student_view";
}
@RequestMapping(value="/delete/{id}", method = RequestMethod.GET)
public String delete(Model model, @PathVariable Integer id){
studentService.delete(id);
return "redirect:/student";
}
}
执行结果: