#/cloudsafer/src/config/jdbc.properties
jdbc.driverClass=com.mysql.jdbc.Driver
jdbc.jdbcUrl=jdbc\:mysql\://localhost\:3306/cloudsafer
jdbc.user=root
jdbc.password=rootabc
c3p0.minPoolSize=5
c3p0.maxPoolSize=30
c3p0.initialPoolSize=10
c3p0.maxIdleTime=60
c3p0.acquireIncrement=3
c3p0.idleConnectionTestPeriod=60
c3p0.acquireRetryAttempts=30
c3p0.breakAfterAcquireFailure=true
hibernate.dialect=org.hibernate.dialect.MySQL5Dialect
hibernate.show_sql=true
hibernate.format_sql=true
hibernate.hbm2ddl.auto=create
#/cloudsafer/src/config/log4j.properties
log4j.rootLogger=INFO,stdout,file
#\u5E94\u7528\u4E8E\u63A7\u5236\u53F0
log4j.appender.stdout=org.apache.log4j.ConsoleAppender
log4j.appender.stdout.layout=org.apache.log4j.PatternLayout
log4j.appender.stdout.Target=System.out
log4j.appender.stdout.layout.ConversionPattern=%-d{yyyy-MM-dd HH\:mm\:ss,SSS} [%c]-[%p] %m%n
#\u5E94\u7528\u4E8E\u6587\u4EF6
log4j.appender.file=org.apache.log4j.DailyRollingFileAppender
log4j.appender.file.File=${spring_springmvc_jpa.root}/app.log
log4j.appender.file.layout=org.apache.log4j.PatternLayout
log4j.appender.R.layout.ConversionPattern=%d{yyyy-MM-dd HH:mm:ss,SSS} [%t] [%c] [%p] - %m%n
log4j.logger.org.hibernate.tool.hbm2ddl=debug
#/cloudsafer/src/config/messages.properties
username.not.empty=\u7528\u6237\u59D3\u540D\u4E0D\u80FD\u4E3A\u7A7A
#
#/cloudsafer/src/cn/nudt/cloudsafer/repository/UserRepository.java
package cn.nudt.cloudsafer.repository;
import org.springframework.data.jpa.repository.JpaRepository;
import cn.nudt.cloudsafer.entity.User;
public interface UserRepository extends JpaRepository<User, Long> {}
#/cloudsafer/src/cn/nudt/cloudsafer/entity/User.java
#
package cn.nudt.cloudsafer.entity;
import java.util.Date;
import java.util.List;
import javax.persistence.CascadeType;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.ManyToOne;
import javax.persistence.OneToMany;
import javax.persistence.OneToOne;
import javax.persistence.Table;
import javax.persistence.Temporal;
import javax.persistence.TemporalType;
@Entity
@Table(name = "t_user")
public class User {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
@Column(name = "userId")
private Integer userId;
@Column(name = "username")
private String username;
@Column(name = "email")
private String email;
@Column(name = "password")
private String password;
@Column(name = "level")
private Integer level;
@Temporal(TemporalType.DATE)
@Column(name = "registerdate")
private Date registerdate;
@OneToOne(cascade = CascadeType.ALL, mappedBy = "user")
private UserInfo userinfo;
@OneToMany(cascade = CascadeType.ALL, mappedBy = "user")
private List<CreditMachine> machines;
@OneToMany(cascade = CascadeType.ALL, mappedBy = "user")
private List<EncryptionFile> files;
public Integer getUserId() {
return userId;
}
public void setUserId(Integer userId) {
this.userId = userId;
}
public String getUsername() {
return username;
}
public void setUsername(String username) {
this.username = username;
}
public String getEmail() {
return email;
}
public void setEmail(String email) {
this.email = email;
}
public String getPassword() {
return password;
}
public void setPassword(String password) {
this.password = password;
}
public Integer getLevel() {
return level;
}
public void setLevel(Integer level) {
this.level = level;
}
public Date getRegisterdate() {
return registerdate;
}
public void setRegisterdate(Date registerdate) {
this.registerdate = registerdate;
}
public UserInfo getUserinfo() {
return userinfo;
}
public void setUserinfo(UserInfo userinfo) {
this.userinfo = userinfo;
}
public List<CreditMachine> getMachines() {
return machines;
}
public void setMachines(List<CreditMachine> machines) {
this.machines = machines;
}
public List<EncryptionFile> getFiles() {
return files;
}
public void setFiles(List<EncryptionFile> files) {
this.files = files;
}
@ManyToOne
private Administer admin;
public Administer getAdmin() {
return admin;
}
public void setAdmin(Administer admin) {
this.admin = admin;
}
}
#/cloudsafer/src/cn/nudt/cloudsafer/service/UserService.java
#
package cn.nudt.cloudsafer.service;
import javax.transaction.Transactional;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Repository;
import cn.nudt.cloudsafer.entity.User;
import cn.nudt.cloudsafer.repository.UserRepository;
@Repository
@Transactional
class UserServiceImpl {
@Autowired
private UserRepository repository;
@Transactional
public User save(User user) {
return repository.save(user);
}
}
/cloudsafer/src/cn/nudt/cloudsafer/controller/UserController.java
#
package cn.nudt.cloudsafer.controller;
import java.io.File;
import java.io.IOException;
import javax.servlet.http.HttpServletRequest;
import org.apache.commons.io.FileUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpHeaders;
import org.springframework.http.HttpStatus;
import org.springframework.http.MediaType;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.servlet.ModelAndView;
@Controller
public class UserController {
@Autowired
private HttpServletRequest request;
private final String uploadTempDir = "WEB-INF/fileUpload/";
@RequestMapping("hello")
public ModelAndView hello() {
ModelAndView mav = new ModelAndView("hellospringmvc");
mav.addObject("name", "fengtao");
return mav;
}
@RequestMapping("fileUpload")
public String fileUpload(@RequestParam("file") MultipartFile file) {
// 判断文件是否为空
if (!file.isEmpty()) {
try {
// 文件保存路径
String filePath = request.getSession().getServletContext()
.getRealPath("/")
+ uploadTempDir + file.getOriginalFilename();
// 转存文件
file.transferTo(new File(filePath));
} catch (Exception e) {
e.printStackTrace();
}
}
// 重定向
return "redirect:/list";
}
@RequestMapping("list")
public ModelAndView list() {
String fileDir = request.getSession().getServletContext()
.getRealPath("/")
+ uploadTempDir;
ModelAndView mav = new ModelAndView("list");
File uploadDest = new File(fileDir);
String[] fileNames = uploadDest.list();
mav.addObject("fileNames", fileNames);
return mav;
}
@RequestMapping("download")
public ResponseEntity<byte[]> download(
@RequestParam(value = "fileName") String fileName)
throws IOException {
HttpHeaders headers = new HttpHeaders();
headers.setContentType(MediaType.APPLICATION_OCTET_STREAM);
headers.setContentDispositionFormData("attachment", fileName);
String fileDir = request.getSession().getServletContext()
.getRealPath("/")+ uploadTempDir;
File downFile = new File(fileDir
+ new String(fileName.getBytes("ISO8859-1"), "UTF-8"));
return new ResponseEntity<byte[]>(
FileUtils.readFileToByteArray(downFile), headers,
HttpStatus.CREATED);
}
}
#/cloudsafer/src/test/UserTest.java
#
package test;
import java.io.UnsupportedEncodingException;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import org.junit.BeforeClass;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
import cn.nudt.cloudsafer.entity.User;
import cn.nudt.cloudsafer.repository.UserRepository;
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(locations = { "classpath*:config/applicationContext.xml",
"classpath*:config/springMVC-servlet.xml" })
public class UserTest {
@Autowired
private UserRepository userDao;
@BeforeClass
public static void setUpBeforeClass() throws Exception {
}
@Test
public void test() {
User u = new User();
。。。
userDao.save(u);
}
转载于:https://my.oschina.net/nck/blog/402642