我们现在开发都是用框架技术,用框架比较方便.可以节省我们很多时间.我用了三大框架写了一个程序,使用maven导的包.大家可以一起学习学习,指点指点.
只是项目结构:
使用框架技术,我们首先就要导包.我是用的是maven导的包 pom.xml
这个是数据库的脚本
create database ssm;
use ssm;
drop table if exists student;
create table student(
id char(40) primary key,
sname varchar(100) not null,
age int
)engine=innodb default charset=utf8;
insert into student values('1','掌声',22);
jdnc的连接
jdbc.driverClassName=com.mysql.jdbc.Driver
jdbc.url=jdbc\:mysql\://localhost\:3306/ssm?useUnicode\=true&characterEncoding\=UTF-8
jdbc.username=root
jdbc.password=a
jdbc.maxActive=150
jdbc.minIdle=5
jdbc.maxIdle=20
beans.xml 配置信息
beans-web.xml 是web层的配置
struts.xml是struts的控制层
/add_success.jsp
package com.yc.ssm.bean;
import java.io.Serializable;
public class Student implements Serializable {
/**
*
*/
private static final long serialVersionUID = -8480199388973683250L;
private String id;
private String sname;
private Integer age;
@Override
public String toString() {
return "Student [id=" + id + ", sname=" + sname + ", age=" + age + "]";
}
public String getId() {
return id;
}
public void setId(String id) {
this.id = id;
}
public String getSname() {
return sname;
}
public void setSname(String sname) {
this.sname = sname;
}
public Integer getAge() {
return age;
}
public void setAge(Integer age) {
this.age = age;
}
public Student(String id, String sname, Integer age) {
super();
this.id = id;
this.sname = sname;
this.age = age;
}
public Student() {
super();
}
}
StdeuntBiz接口
package com.yc.ssm.biz;
import com.sun.tools.javac.util.List;
import com.yc.ssm.bean.Student;
public interface StudentBiz {
public int delete(String id);
public void update(Student student);
public Student findById(String id);
public int add(Student student);
public List findAll();
}
接口的实现类
package com.yc.ssm.biz.impl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.sun.tools.javac.util.List;
import com.yc.ssm.bean.Student;
import com.yc.ssm.biz.StudentBiz;
import com.yc.ssm.dao.StudentDao;
//使用注解注入
@Service("studentBiz")
public class StudentBizImpl implements StudentBiz{
@Autowired
public StudentDao studentDao;
@Override
public int delete(String id) {
return studentDao.delete(id);
}
public void setStudentDao(StudentDao studentDao) {
this.studentDao = studentDao;
}
@Override
public void update(Student student) {
studentDao.update(student);
}
@Override
public Student findById(String id) {
return studentDao.findById(id);
}
@Override
public int add(Student student) {
return studentDao.add(student);
}
@Override
public List findAll() {
return null;
}
}
StudentDao类
package com.yc.ssm.dao;
import com.yc.ssm.bean.Student;
public interface StudentDao {
public int delete(String id);
public void update(Student student);
public Student findById(String id);
public int add(Student student);
}
delete from student where id=#{id}
insert into student(id,sname,age) values (#{id},#{sname},#{age})
update student set sname=#{sname},age=#{age} where id=#{id}
package com.yc.ssm.web.action;
import java.util.ArrayList;
import java.util.Map;
import java.util.UUID;
import org.apache.struts2.interceptor.SessionAware;
import com.opensymphony.xwork2.ActionSupport;
import com.opensymphony.xwork2.ModelDriven;
import com.sun.tools.javac.util.List;
import com.yc.ssm.bean.Student;
import com.yc.ssm.biz.StudentBiz;
public class StudentAction extends ActionSupport implements ModelDriven,SessionAware {
private static final long serialVersionUID = -6376764196089300549L;
private StudentBiz studentBiz;
private Student student;
private Map session;
public void setStudent(Student student) {
this.student = student;
}
public Student getModel() {
student =new Student();
return student;
}
public String doAdd(){
student.setId(UUID.randomUUID().toString());
System.out.println(student.toString());
System.out.println(studentBiz);
studentBiz.add(student);
return "add_success";
}
public String doUpdate(){
this.studentBiz.update(student);
return super.SUCCESS;
}
public String del(){
this.studentBiz.delete(student.getId());
return super.SUCCESS;
}
public String findAll(){
List list=this.studentBiz.findAll();
this.session.put("list", list);
return super.SUCCESS;
}
public void setStudentBiz(StudentBiz studentBiz){
this.studentBiz=studentBiz;
}
public void setSession(Map arg0){
this.session=arg0;
}
}
package com.yc.ssmTest;
import static org.junit.Assert.*;
import java.util.UUID;
import org.junit.After;
import org.junit.Before;
import org.junit.Test;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import com.yc.ssm.bean.Student;
import com.yc.ssm.biz.StudentBiz;
public class test {
private StudentBiz studentBiz;
@Before
public void before(){
ApplicationContext ac=new ClassPathXmlApplicationContext("beans.xml");
studentBiz=(StudentBiz) ac.getBean("studentBiz");
}
@Test
public void TestAdd(){
Student s=new Student();
s.setId(UUID.randomUUID().toString());
s.setAge(22);
s.setSname("得分");
studentBiz.add(s);
}
@Test
public void TestfindById(){
studentBiz.findById("1");
}
}
<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="utf-8"%>
添加
添加学生
add_success.jsp
<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="utf-8"%>
添加成功
添加成功