创新互联建站专注于黄南州网站建设服务及定制,我们拥有丰富的企业做网站经验。 热诚为您提供黄南州营销型网站建设,黄南州网站制作、黄南州网页设计、黄南州网站官网定制、微信小程序服务,打造黄南州网络公司原创品牌,更为您提供黄南州网站排名全网营销落地服务。
1.在pom.xml中添加如下依赖:
UTF-8
4.3.5.RELEASE
4.3.8.Final
2.5.0
org.springframework
spring-core
${spring.version}
org.springframework
spring-beans
${spring.version}
org.springframework
spring-context
${spring.version}
org.springframework
spring-tx
${spring.version}
org.springframework
spring-webmvc
${spring.version}
org.springframework
spring-web
${spring.version}
org.springframework
spring-test
${spring.version}
provided
org.springframework
spring-orm
${spring.version}
org.hibernate
hibernate-core
${hibernate.version}
org.hibernate
hibernate-ehcache
${hibernate.version}
mysql
mysql-connector-java
5.1.34
commons-dbcp
commons-dbcp
1.4
commons-pool
commons-pool
1.6
log4j
log4j
1.2.17
org.aspectj
aspectjweaver
1.8.4
com.fasterxml.jackson.core
jackson-annotations
${jackson.version}
com.fasterxml.jackson.core
jackson-core
${jackson.version}
com.fasterxml.jackson.core
jackson-databind
${jackson.version}
junit
junit
4.11
test
在build节点中添加如下代码:
src/main/java
**/*.properties
**/*.xml
false
2.src/main/resources/datasource.properties
datasource.driverClassName=com.mysql.jdbc.Driver
datasource.userName=root
datasource.userPassword=123456
datasource.url=jdbc:mysql://127.0.0.1:3306/test?useUnicode=true&characterEncoding=UTF-8&useSSL=false
hibernate.dialect = org.hibernate.dialect.MySQLDialect
hibernate.show_sql = true
hibernate.format_sql = true
hibernate.hbm2ddl.auto = update
3.src/main/resources/log4j.properties
#定义LOG输出级别
log4j.rootLogger=INFO,Console,File
#定义日志输出目的地为控制台
log4j.appender.Console=org.apache.log4j.ConsoleAppender
log4j.appender.Console.Target=System.out
#可以灵活的指定日志输出格式,下面一行是指定具体的格式
log4j.appender.Console.layout=org.apache.log4j.PatternLayout
log4j.appender.Console.layout.ConversionPattern=[%c]-%m%n
#mybatis显示SQL语句日志配置 net.cxp.blog.dao是你的dao接口所在的包名
#log4j.logger.org.mybatis=DEBUG
log4j.logger.com.lymn.it.mapper=DEBUG
#文件大小到达指定尺寸的时候产生一个新的文件
log4j.appender.File=org.apache.log4j.RollingFileAppender
#指定输出目录
log4j.appender.File.File=d:/logs/ssm.log
#定义文件最大大小
log4j.appender.File.MaxFileSize=10MB
#输出所有日志,如果换成DEBUG表示输出DEBUG以上级别日志
log4j.appender.File.Threshold=DEBUG
log4j.appender.File.layout=org.apache.log4j.PatternLayout
log4j.appender.File.layout.ConversionPattern=[%p][%d{yyyy-MM-dd HH\:mm|\:ss}][%c]%m%n
4.src/main/resources/spring-mvc.xml
5.src/main/resources/spring-hibernate.xml
${hibernate.hbm2ddl.auto}
${hibernate.dialect}
${hibernate.show_sql}
${hibernate.format_sql}
6.web.xml
contextConfigLocation
classpath:spring-hibernate.xml
org.springframework.web.context.ContextLoaderListener
spring-mvc
org.springframework.web.servlet.DispatcherServlet
contextConfigLocation
classpath:spring-mvc.xml
1
spring-mvc
/
org.springframework.web.util.IntrospectorCleanupListener
encodingFilter
org.springframework.web.filter.CharacterEncodingFilter
encoding
utf-8
forceEncoding
true
encodingFilter
/*
7.DAO层之UserDao、UserDaoImpl
package com.lymn.it.dao;
import com.lymn.it.model.User;
public interface UserDao {
public void save(User user);
public User get(Integer uid);
}
package com.lymn.it.dao.impl;
import javax.annotation.Resource;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.springframework.stereotype.Repository;
import com.lymn.it.dao.UserDao;
import com.lymn.it.model.User;
@Repository
public class UserDaoImpl implements UserDao{
@Resource
SessionFactory sessionFactory;
@Override
public void save(User user) {
Session session = sessionFactory.openSession();
session.save(user);
}
@Override
public User get(Integer uid) {
Session session = sessionFactory.openSession();
User object = (User) session.get(User.class, uid);
return object;
}
}
8.Service层之UserService、UserServiceImpl
package com.lymn.it.service;
import com.lymn.it.model.User;
public interface UserService {
public void save(User user);
public User get(Integer uid);
}
package com.lymn.it.service.impl;
import javax.annotation.Resource;
import org.springframework.stereotype.Service;
import com.lymn.it.dao.UserDao;
import com.lymn.it.model.User;
import com.lymn.it.service.UserService;
@Service
public class UserServiceImpl implements UserService{
@Resource
UserDao userDao;
@Override
public void save(User user) {
userDao.save(user);
}
@Override
public User get(Integer uid) {
// TODO Auto-generated method stub
return userDao.get(uid);
}
}
9.Controller层之UserController
package com.lymn.it.controller;
import java.util.Map;
import javax.annotation.Resource;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import com.lymn.it.model.User;
import com.lymn.it.service.UserService;
@Controller
public class UserController {
@Resource
UserService userService;
@RequestMapping(value="/saveUser")
public String saveUser(User user){
userService.save(user);
return "saveSucess";
}
@RequestMapping(value="/getUser")
public String getUser(Map map){
User user=userService.get(3);
map.put("user", user);
return "getSucess";
}
}
10.Model层之User
package com.lymn.it.model;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.Id;
import javax.persistence.Table;
@Entity
@Table(name="t_user")
public class User {
private Integer id;
private String name;
private String password;
@Id
@GeneratedValue
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 String getPassword() {
return password;
}
public void setPassword(String password) {
this.password = password;
}
@Override
public String toString() {
return "User [id=" + id + ", name=" + name + ", password=" + password
+ "]";
}
}
jsp/getSucess.jsp
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
Insert title here
${user}
访问如下图所示代表成功