SSM整合:
导入依赖:
4.0.0
cn.itcast.parent
itcast-parent
0.0.1-SNAPSHOT
cn.itcast.usermanage
itcast-usermanage
1.0.0-SNAPSHOT
war
junit
junit
test
org.springframework
spring-webmvc
org.springframework
spring-jdbc
org.springframework
spring-aspects
org.mybatis
mybatis
org.mybatis
mybatis-spring
com.github.miemiedev
mybatis-paginator
mysql-connector-java
com.jolbox
bonecp
org.slf4j
slf4j-log4j12
com.fasterxml.jackson.core
jackson-databind
jstl
jstl
javax.servlet
servlet-api
provided
javax.servlet
jsp-api
provided
joda-time
joda-time
org.apache.commons
commons-lang3
org.apache.commons
commons-io
com.github.pagehelper
pagehelper
com.github.jsqlparser
jsqlparser
commons-fileupload
commons-fileupload
org.apache.tomcat.maven
tomcat7-maven-plugin
8089
/
在web.xml中进行spring和springmvc的配置,需要的是springmvc的核心前端控制器和spring的核心监听器。
Web.xml
mjf-usermanage
contextConfigLocation
classpath:spring/applicationContext*.xml
org.springframework.web.context.ContextLoaderListener
encoding
org.springframework.web.filter.CharacterEncodingFilter
encoding
UTF-8
encoding
/*
springmvc
org.springframework.web.servlet.DispatcherServlet
contextConfigLocation
classpath:spring/springmvc-servlet.xml
1
springmvc
/
index.jsp
还有就是各种配置文件,如mybatis-config.xml和UserMapper.xml还有springmvc-servlet.xml和applicationContext.xml
mybatis-config.xml:
UserMapper.xml:
select * from tb_user;
insert into tb_user(
user_name,
password,
name,
age,
sex,
birthday
)
VALUES(
#{userName},
#{password},
#{name},
#{age},
#{sex},
#{birthday}
);
delete from tb_user where id=#{id};
select * from tb_user where id = #{id};
update tb_user set
user_name = #{userName},
password = #{password},
name = #{name},
age = #{age},
sex = #{sex},
birthday = #{birthday}
where id = #{id}
Springmvc-servlet.xml:
applicationContext.xml:
classpath:jdbc.properties
各种的配置准备好了,就是需要定义mvc三层开发结构了。
包括Action、Dao和Service层
Action:
package mjf.haihan.usermanager.controller;
import java.util.List;
import mjf.haihan.usermanager.pojo.User;
import mjf.haihan.usermanager.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;
@RequestMapping("user")
@Controller
public class UserAction {
@Autowired
private UserService userService;
@RequestMapping("users")
public ModelAndView queryAll(){
ModelAndView mv = new ModelAndView("users");
List users = userService.queryAll();
mv.addObject("users", users);
return mv;
}
@RequestMapping("adduser")
public String saveUser(User user){
userService.saveUser(user);
return "redirect:/user/users";
}
@RequestMapping("deleteuser")
public String deleteuser(Long id){
userService.deleteUser(id);
return "redirect:/user/users";
}
@RequestMapping("edituser")
public ModelAndView editUser(Long id){
ModelAndView mv = new ModelAndView("edit");
User user = userService.queryById(id);
mv.addObject("user", user);
return mv;
}
@RequestMapping("edit")
public String edit(User user){
userService.edit(user);
return "forward:/user/users";
}
}
Dao\Mapper:
package mjf.haihan.usermanager.mapper;
import java.util.List;
import mjf.haihan.usermanager.pojo.User;
public interface UserMapper {
/**查询所有用户*/
public List queryAll();
/**添加用户*/
public void saveUser(User user);
/**删除用户 */
public void deletUser(Long id);
/**跳转到修改页面 */
public User queryById(Long id);
/**修改用户*/
public void edit(User user);
}
Service:
package mjf.haihan.usermanager.service;
import java.util.Date;
import java.util.List;
import mjf.haihan.usermanager.mapper.UserMapper;
import mjf.haihan.usermanager.pojo.User;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
@Service
public class UserService {
@Autowired
private UserMapper userMapper;
public List queryAll(){
List users = userMapper.queryAll();
return users;
}
public void saveUser(User user){
user.setId(null);
user.setCreated(new Date());
user.setUpdated(user.getCreated());
userMapper.saveUser(user);
}
public void deleteUser(Long id) {
userMapper.deletUser(id);
}
public User queryById(Long id) {
User user = userMapper.queryById(id);
return user;
}
public void edit(User user) {
// TODO Auto-generated method stub
userMapper.edit(user);
}
}
这个是简单的用户crud的web项目。