思路:
思路:
依赖jar包
DDL
CREATE TABLE `t_user` (
`id` bigint(20) NOT NULL AUTO_INCREMENT,
`username` varchar(255) DEFAULT NULL,
`password` varchar(255) DEFAULT NULL,
`age` int(11) DEFAULT NULL,
PRIMARY KEY (`id`)
) ENGINE=InnoDB AUTO_INCREMENT=7 DEFAULT CHARSET=utf8;
Mapper文件
UserMapper.xml
insert into t_user (username,password,age)
values (#{username},#{password},#{age});
update t_user
set
username=#{username},
age=#{age}
where
id=#{id}
delete from t_user where id=#{id}
Spring配置文件
applicationContext.xml
资源文件
db.properties
jdbc.url=jdbc:mysql:///ssm?useSSL=false
jdbc.username=root
jdbc.password=admin
mybatis文件
log4j.properties
#全局的日志配置
log4j.rootLogger=ERROR,stdout
#MyBatis的日志配置,打印哪些名称空间开头的日志
log4j.logger.cn.dusk.ssm.mapper=TRACE
#打印日志的地方
log4j.appender.stdout=org.apache.log4j.ConsoleAppender
log4j.appender.stdout.layout=org.apache.log4j.PatternLayout
log4j.appender.stdout.layout.ConversionPattern=%5p [%t] - %m%n
mvc
mvc.xml
mybatis.xml
登录控制器
LoginController
@Controller
public class LoginController {
@RequestMapping("login")
public String login(String username,String password,HttpSession session){
if ("admin".equals(username) && "123".equals(password)){
session.setAttribute("USER_IN_SESSION",username);
return "forward:/user/list.do";
}
return "forward:/login.jsp";
}
}
页面
UserController
@Controller
@RequestMapping("user")
public class UserController {
@Autowired
private IUserService userService;
@RequestMapping("list")
public String list(Model model){
List list = userService.list();
model.addAttribute("list",list);
return "user/list";
}
@RequestMapping("input")
public String input(Long id,Model model){
if (id != null) {
User user = userService.get(id);
model.addAttribute("u",user);
}
return "user/input";
}
@RequestMapping("saveOrUpdate")
public String saveOrUpdate(User u){
if (u.getId() == null) {
userService.save(u);
}else{
userService.update(u);
}
return "redirect:/user/list.do";
}
@RequestMapping("delete")
public String delete(Long id){
if (id != null) {
userService.delete(id);
}
return "redirect:/user/list.do";
}
}
实体类User
@Setter
@Getter
@ToString
@AllArgsConstructor
@NoArgsConstructor
public class User {
private Long id;
private String username;
private String password;
private Integer age;
}
UserMapper
public interface UserMapper {
void insert(User user);
void updateByPrimaryKey(User user);
void deleteByPrimaryKey(Long id);
User selectByPrimaryKey(Long id);
List selectAll();
}
服务接口
IUserService
public interface IUserService {
void save(User user);
void update(User user);
void delete(Long id);
User get(Long id);
List list();
}
service实现类UserServiceImpl
@Service
public class UserServiceImpl implements IUserService {
/**
* 依赖mapper
* session=factory.openSession();//先拿到sessionFactory
* userMapper=session.getMapper(UserMapper.class);//在拿到代理对象
*/
@Autowired
private UserMapper userMapper;
public void save(User user) {
userMapper.insert(user);
}
public void update(User user) {
userMapper.updateByPrimaryKey(user);
}
public void delete(Long id) {
userMapper.deleteByPrimaryKey(id);
}
public User get(Long id) {
User user = userMapper.selectByPrimaryKey(id);
return user;
}
public List list() {
List list = userMapper.selectAll();
return list;
}
}
控制器CheckLoginInterceptor
public class CheckLoginInterceptor extends HandlerInterceptorAdapter{
/*在controller方法值前做拦截,返回true表示放行*/
public boolean preHandle(HttpServletRequest request,
HttpServletResponse response,Object handler) throws IOException {
//从session中获取当前登录成功的对象
Object user = request.getSession().getAttribute("USER_IN_SESSION");
//判断是否为null
if (user == null) {
//没有登录,返回到登录界面
response.sendRedirect("/login.jsp");
return false;
}
return true;//放行
}
}
测试类
App
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration("classpath:applicationContext.xml")
public class App {
@Autowired
private IUserService userService;
@Test
public void testSave(){
User u = new User(null, "Dusk", "1234", 28);
userService.save(u);
}
@Test
public void testUpdate(){
User u = new User(2L, "Dusk.Decade", "123123", 259);
userService.update(u);
}
@Test
public void testDelete(){
userService.delete(1L);
}
@Test
public void testGet(){
User user = userService.get(1L);
System.out.println(user);
}
@Test
public void testList(){
List list = userService.list();
for (User user : list) {
System.out.println(user);
}
}
}
添加或编辑列表
input.jsp
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
信息列表
list.jsp
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
信息列表
新增
编号---> 姓名---> 密码--->年龄---->操作
${u.id} ---> ${u.username} ---> ${u.password} ---> ${u.age}
编辑
删除
web.xml
CharacterEncodingFilter
org.springframework.web.filter.CharacterEncodingFilter
encoding
UTF-8
forceEncoding
true
CharacterEncodingFilter
/*
mvc
org.springframework.web.servlet.DispatcherServlet
contextConfigLocation
classpath:applicationContext.xml
1
mvc
*.do
login.jsp
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
登录