今天学习了企业项目框架的整合,基本框架如下:
首先和上次一样建一个maven项目,不过这次再创建时不用勾选CREATE FROM ARCHETYPE,也就不用勾选webapp了,这个项目命名为ssm_parent,作为父项目。
然后在这个父项目下新建4个子项目bean,dao,service,web,选择new,点击module,也不勾选CREATE FROM ARCHETYPE,点击next,在add as module to 选择ssm_parent,下面也选这个,id分别为com.zhongruan和bean,dao和service也用同样方法新建,在新建wep时就要勾选CREATE FROM ARCHETYPE了,并选择webapp,其他步骤一样。
在bean下的java中建包com.zhongruan.bean,然后在bean包里建类UserInfo,代码如下:
package com.zhongruan.bean;
public class UserInfo {
private int id;
private String username;
private String password;
public int getId() {
return id;
}
public void setId(int 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 "UserInfo{" +
"id=" + id +
", username='" + username + '\'' +
", password='" + password + '\'' +
'}';
}
}
在dao下的java中建包com.zhongruan.dao,在dao包里面建IUserDao接口,代码如下:
package com.zhongruan.dao;
import com.zhongruan.bean.UserInfo;
import java.util.List;
public interface IUserDao {
public List<UserInfo> findAll();
public UserInfo doLogin(UserInfo userInfo);
}
在resources下建mapper,在mapper下建usermapper.xml,代码如下:
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd" >
<mapper namespace="com.zhongruan.dao.IUserDao" >
<select id="findAll" resultType="com.zhongruan.bean.UserInfo">
select * from userinfo
</select>
<select id="doLogin" parameterType="com.zhongruan.bean.UserInfo" resultType="com.zhongruan.bean.UserInfo">
select * from userinfo where username=#{username} and password=#{password}
</select>
</mapper>
在service下的java里建包com.zhongruan.service,在service包里建IUserService接口,代码如下:
package com.zhongruan.service;
import com.zhongruan.bean.UserInfo;
import java.util.List;
public interface IUserService {
public List<UserInfo> findAll();
public Boolean doLogin(UserInfo userInfo);
}
在service包里新建包impl,在impl包里定义类UserService,代码如下:
package com.zhongruan.service.impl;
import com.zhongruan.bean.UserInfo;
import com.zhongruan.dao.IUserDao;
import com.zhongruan.service.IUserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import java.util.List;
@Service
public class UserService implements IUserService {
@Autowired
private IUserDao userDao;
@Override
public List<UserInfo> findAll() {
return userDao.findAll();
}
@Override
public Boolean doLogin(UserInfo userInfo) {
UserInfo userInfo1 = userDao.doLogin(userInfo);
if(null!=userInfo1){
return true;
}else {
return false;
}
}
}
在web下面建com.zhongruan.controller包,在controller包里定义UserController类,代码如下:
package com.zhongruan.controller;
import com.zhongruan.bean.UserInfo;
import com.zhongruan.service.IUserService;
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;
import java.util.List;
@Controller
@RequestMapping("user")
public class UserController {
@Autowired
private IUserService userService;
@RequestMapping("findAll.do")
public ModelAndView findAll(){
List<UserInfo> infos = userService.findAll();
ModelAndView mv=new ModelAndView();
mv.addObject("infos",infos);
mv.setViewName("user-list");
return mv;
}
@RequestMapping("login.do")
public ModelAndView dologin(UserInfo userInfo){
Boolean flag = userService.doLogin(userInfo);
ModelAndView modelAndView=new ModelAndView();
if(flag){
modelAndView.setViewName("main");
}else{
modelAndView.setViewName("../failer");
}
return modelAndView;
}
}
在resources里面拷入applicationContext.xml,db.properties,log4j.properties,spring-mvc.xml,代码上次已经给出,这里就不在赘述了。
在webapp下拷入css,img,pages,pligins,failer.jsp,index.jsp,login.jsp,这些文件可以自己写,也可以去下载得到。
最后不要忘了配置ssm_parent的pom.xml文件。