基于注解的简单SSH保存用户小案例(2)

#设置日志记录到控制台的方式
log4j.appender.std=org.apache.log4j.ConsoleAppender
#out以黑色字体输出,err以红色字体输出
log4j.appender.std.Target=System.err
log4j.appender.std.layout=org.apache.log4j.PatternLayout
log4j.appender.std.layout.ConversionPattern=%d{yyyy-MM-dd HH:mm:ss} %5p %c{1}:%L - %m%n
#设置日志记录到文件的方式
log4j.appender.file=org.apache.log4j.FileAppender
#日志文件路径文件名
log4j.appender.file.File=mylog.log
#日志输出的格式
log4j.appender.file.layout=org.apache.log4j.PatternLayout
log4j.appender.file.layout.ConversionPattern=%d{ABSOLUTE} %5p %c{1}:%L - %m%n
#日志输出的级别,以及配置记录方案,级别:error > warn > info>debug>trace
log4j.rootLogger=info, std, file

六、页面代码

index.jsp

<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
  <head>
    <title>$Title$</title>
  </head>
  <body>
  <h3>添加客户</h3>
  <form action="${ pageContext.request.contextPath }/customerAction_save" method="post">
      姓名:<input type="text" /><br/>
      年龄:<input type="text" /><br/>
      <input type="submit" value="提交"/><br/>
  </form>
  </body>
</html>

success.jsp

<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
    <title>首页</title>
</head>
<body>
    <h1>保存成功!</h1>
</body>
</html>

七、实体层代码

package com.pri.domain;

import javax.persistence.*;


@Entity
@Table(name = "customer")
public class Customer {
    @Id
    @Column(name = "userId")
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    private Integer userId;

@Column(name = "name")
    private String name;
    @Column(name = "age")
    private Integer age;

public Integer getUserId() {return userId; }

public void setUserId(Integer userId) {this.userId = userId;}

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;}
}

八、action层代码

package com.pri.action;

import com.pri.domain.Customer;
import com.pri.service.CustomerService;
import com.opensymphony.xwork2.ActionSupport;
import com.opensymphony.xwork2.ModelDriven;
import org.apache.struts2.convention.annotation.Action;
import org.apache.struts2.convention.annotation.Namespace;
import org.apache.struts2.convention.annotation.ParentPackage;
import org.apache.struts2.convention.annotation.Result;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Controller;
import javax.annotation.Resource;


@Controller("customerAction")
@ParentPackage(value = "struts-default")
@Scope("prototype")
@Namespace(value = "/")
public class CustomerAction extends ActionSupport implements ModelDriven<Customer>{

private Customer customer;

@Resource(name = "customerService")
    private CustomerService customerService;

@Override
    public Customer getModel() {
        if (customer == null ){
            customer = new Customer();
        }
        return customer;
    }

@Action(value = "customerAction_save",
            results = {@Result(name = SUCCESS,type = "redirect",location = "/success.jsp")})
    public String save(){
        customerService.save(customer);
        return SUCCESS;
    }
}

九、service层代码

package com.pri.service;

import com.pri.domain.Customer;

public interface CustomerService {
    void save(Customer user);
}
//=======================================
package com.pri.service.impl;

import com.pri.domain.Customer;
import com.pri.dao.CustomerDao;
import com.pri.service.CustomerService;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;

@Service("customerService")
@Transactional
public class CustomerServiceImpl implements CustomerService {

@Resource(name = "customerDao")
    private CustomerDao customerDao;

@Override
    public void save(Customer customer) {
        customerDao.save(customer);
    }
}

十、dao层代码

package com.pri.dao;

import com.pri.domain.Customer;

public interface CustomerDao {
    void save(Customer customer);
}

//==================================================================
package com.pri.dao.impl;

内容版权声明:除非注明,否则皆为本站原创文章。

转载注明出处:https://www.heiqu.com/7d585a3e47e45916248b2b654f0df157.html