1、创建一个maven项目
2、pom.xml导入依赖jar包(这里需要注意的是jar包的完整性,不然会报错)
Junit,spring-test:用于单元测试(不测试可以不加);spring-data-jpa(1.11.3);spring-webmvc(4.3.8);hibernate-core(5.0.11);hibernate-entitymanager(5.0.11);javax.servlet-api(3.1.0);javax.servet.jsp-api(2.2.1);Jstl(1.2),standard(1.1.2):在项目中使用JSTL和EL表达式;mysql-connector-java(5.1.29):数据库驱动;c3p0(0.9.5);jackson-databind(2.9.6):用于数据返回(版本太低不支持);slf4j-log4j12(1.6.4)
3、创建数据库驱动文件:db.properties
jdbc.jdbcUrl=jdbc:mysql://localhost:3306/sss
jdbc.driverClass=com.mysql.jdbc.Driver
jdbc.user=root
jdbc.password=root
4、创建打印日志的文件:log4j.properties
log4j.rootLogger=INFO,Console,File
#\u5B9A\u4E49\u65E5\u5FD7\u8F93\u51FA\u76EE\u7684\u5730\u4E3A\u63A7\u5236\u53F0
log4j.appender.Console=org.apache.log4j.ConsoleAppender
log4j.appender.Console.Target=System.out
#\u53EF\u4EE5\u7075\u6D3B\u5730\u6307\u5B9A\u65E5\u5FD7\u8F93\u51FA\u683C\u5F0F\uFF0C\u4E0B\u9762\u4E00\u884C\u662F\u6307\u5B9A\u5177\u4F53\u7684\u683C\u5F0F
log4j.appender.Console.layout = org.apache.log4j.PatternLayout
log4j.appender.Console.layout.ConversionPattern=[%c] - %m%n
#\u6587\u4EF6\u5927\u5C0F\u5230\u8FBE\u6307\u5B9A\u5C3A\u5BF8\u7684\u65F6\u5019\u4EA7\u751F\u4E00\u4E2A\u65B0\u7684\u6587\u4EF6
log4j.appender.File = org.apache.log4j.RollingFileAppender
#\u6307\u5B9A\u8F93\u51FA\u76EE\u5F55
log4j.appender.File.File = logs/ssm.log
#\u5B9A\u4E49\u6587\u4EF6\u6700\u5927\u5927\u5C0F
log4j.appender.File.MaxFileSize = 10MB
# \u8F93\u51FA\u6240\u4EE5\u65E5\u5FD7\uFF0C\u5982\u679C\u6362\u6210DEBUG\u8868\u793A\u8F93\u51FADEBUG\u4EE5\u4E0A\u7EA7\u522B\u65E5\u5FD7
log4j.appender.File.Threshold = ALL
log4j.appender.File.layout = org.apache.log4j.PatternLayout
log4j.appender.File.layout.ConversionPattern =[%p] [%d{yyyy-MM-dd HH\:mm\:ss}][%c]%m%n
5、创建spring框架配置文件:applicationContext.xml
classpath:db.properties
classpath:log4j.properties
org.hibernate.cfg.ImprovedNamingStrategy
org.hibernate.dialect.MySQLDialect
true
true
update
6、创建springmvc框架配置文件:springmvc.xml
7、web.xml文件中的配置
org.springframework.web.context.ContextLoaderListener
contextConfigLocation
classpath:applicationContext.xml
DispatcherServlet
org.springframework.web.servlet.DispatcherServlet
contextConfigLocation
classpath:springmvc.xml
1
DispatcherServlet
/
characterEncodingFilter
org.springframework.web.filter.CharacterEncodingFilter
encoding
UTF-8
forceEncoding
true
characterEncodingFilter
/*
8、创建实体类(注解模式)(配置了数据库自动创建表,不需要自己创建)
@Entity
@Table(name="user")
public class User {
@Id
@GeneratedValue(strategy=GenerationType.IDENTITY)
private Integer id;
@Column
private String username;//姓名
@Column
private String password;//密码
public Integer getId() {
return id;
}
public void setId(Integer id) {
this.id = id;
}
public String getUsername() {
return username;
}
public void setUsername(String username) {
this.username = username;
}
public String getPassword() {
return password;
}
public void setPassword(String password) {
this.password = password;
}
@Override
public String toString() {
return "User [id=" + id + ", username=" + username + ", password=" + password + "]";
}
}
9、创建dao层,继承JpaRepository,JpaSpecificationExecutor接口
public interface IUserDAO extends JpaRepository,JpaSpecificationExecutor{
@Query("from User where username=?1")
User findOneUserByUsername(String username);
}
10、创建service层(注解注入dao层对象)
@Service
@Transactional
public class UserService implements IUserService{
@Autowired
IUserDAO userDAO;
@Override
public User addUser(User u) {
if (null==u||"".equals(u.getUsername())||"".equals(u.getPassword())) {
return null;
}else {
User user = userDAO.findOneUserByUsername(u.getUsername());
if (null!=user) {
return null;
}else {
return userDAO.save(u);
}
}
}
@Override
public User findUserByUsername(User u) {
if (null!=u&&!"".equals(u.getUsername())&&!"".equals(u.getPassword())) {
User user = userDAO.findOneUserByUsername(u.getUsername());
if (user.getPassword().equals(u.getPassword())) {
return user;
}
}
return null;
}
}
11、创建controller层(注解注入service层对象)
@Controller
@RequestMapping("/user")
public class UserController {
@Autowired
IUserService userService;
@RequestMapping("/regist")
public void regist(Model model,User user,HttpServletRequest request,HttpServletResponse response) throws Exception{
response.setContentType("text/html;charset=utf-8");
User user2 = userService.addUser(user);
if (null!=user2) {
response.getWriter().write("");
}else {
response.getWriter().write("");
}
}
@RequestMapping("/login")
public void login(Model model,User user,HttpServletRequest request,HttpServletResponse response) throws Exception{
response.setContentType("text/html;charset=utf-8");
User user2 = userService.findUserByUsername(user);
if (null!=user2) {
response.getWriter().write("");
}else {
response.getWriter().write("");
}
}
}
这里写一个页面跳转帮助类
@Controller
public class PageController {
@RequestMapping("/page/{page}")
public String skip(@PathVariable(value="page") String page){
return page;
}
}
12、创建前台页面
//注册
//登录