今天学习了ssm框架的搭建,并实现了一个简易的用户管理系统,包含基础的增删查改等功能。以下为搭建过程记录,以及遇到的问题和坑的备忘:
1. 新建一个Maven项目,选择如下模板:
2. 新建以下项目文件夹,并将java标记为代码文件夹,将resources标记为资源文件夹:
3. 配置WEB-INF下的web.xml servlet配置文件:
在applicationContext.xml中配置数据源:
配置springmvc文件:
4. 创建数据库表实体类、控制器类、dao层接口、service层接口、service.impl下接口的具体实现类,并在resources文件夹下创建sql映射mapper文件:
将jsp页面文件copy到WEB-INF的jsp文件下:
记得修改spring-mvc.xml文件中jsp文件的前缀,使其保持一致,否则会产生404错误:
<!-- 3.视图的解析器-->
<bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="prefix" value="/WEB-INF/jsp/"/>
<property name="suffix" value=".jsp"/>
</bean>
5.编写UserInfo实体类:
public class UserInfo {
private int id;
private String username;
private String password;
@Override
public String toString() {
return "UserInfo{" +
"id=" + id +
", username='" + username + '\'' +
", password='" + password + '\'' +
'}';
}
public UserInfo(int id, String username, String password) {
this.id = id;
this.username = username;
this.password = password;
}
public UserInfo() {
}
public int getId() {
return id;
}
public UserInfo setId(int id) {
this.id = id;
return this;
}
public String getUsername() {
return username;
}
public UserInfo setUsername(String username) {
this.username = username;
return this;
}
public String getPassword() {
return password;
}
public UserInfo setPassword(String password) {
this.password = password;
return this;
}
}
6. 编写IUserInfoService接口:
public interface IUserInfoService {
List<UserInfo> findAll();
int save(UserInfo userInfo);
void remove(Integer id);
UserInfo find(Integer id);
void modify(UserInfo userInfo);
}
7.编写dao层的IUserInfoDao接口:
public interface IUserInfoDao {
List<UserInfo> findAll() throws Exception;
void insertAndGetId(UserInfo userInfo) throws Exception;
void deleteById(Integer id) throws Exception;
UserInfo selectById(Integer id) throws Exception;
void updateById(UserInfo userInfo) throws Exception;
}
8.编写IUserInfoService接口的具体实现类IUserInfoServiceImpl:(记得在类上加@Service注解是容器可以扫描到)
@Service
public class UserInfoServiceImpl implements IUserInfoService {
@Autowired
private IUserInfoDao userInfoDao;
@Override
public List<UserInfo> findAll() {
List<UserInfo> users = new ArrayList<>();
try {
users = userInfoDao.findAll();
} catch (Exception e) {
e.printStackTrace();
}
return users;
}
@Override
public int save(UserInfo userInfo) {
try {
userInfoDao.insertAndGetId(userInfo);
return userInfo.getId();
} catch (Exception e) {
e.printStackTrace();
}
return -1;
}
@Override
public void remove(Integer id) {
try {
userInfoDao.deleteById(id);
} catch (Exception e) {
e.printStackTrace();
}
}
@Override
public UserInfo find(Integer id) {
UserInfo userInfo = null;
try {
userInfo = userInfoDao.selectById(id);
} catch (Exception e) {
e.printStackTrace();
}
return userInfo;
}
@Override
public void modify(UserInfo userInfo) {
try {
userInfoDao.updateById(userInfo);
} catch (Exception e) {
e.printStackTrace();
}
}
}
9. 编写控制器层的UserController类,实现从http请求到视图的返回,以及实现页面间的跳转:
@Controller
@RequestMapping("/user")
public class UserController {
@Autowired
IUserInfoService userInfoService;
@RequestMapping("/findAll")
public String findAll(Model mv) {
List<UserInfo> users = userInfoService.findAll();
mv.addAttribute("userInfos", users);
return "allUser";
}
@RequestMapping("/toAddUser")
public String toAddUser(Model model) {
return "addUser";
}
@RequestMapping("/save")
public String saveUser(String username, String password) {
UserInfo userInfo = new UserInfo()
.setUsername(username)
.setPassword(password);
int uid = userInfoService.save(userInfo);
System.out.println("新增用户:" + uid + "." + userInfo);
return "redirect:/user/findAll.do";
}
@RequestMapping("/toUpdate")
public String toUpdate(Integer id,
Model model) {
UserInfo userInfo;
userInfo = userInfoService.find(id);
model.addAttribute("userInfo", userInfo);
return "updateUser";
}
@RequestMapping("/update")
public String updateUser(UserInfo userInfo) {
userInfoService.modify(userInfo);
return "redirect:/user/findAll.do";
}
@RequestMapping("/delete")
public String deleteUser(Integer id) {
userInfoService.remove(id);
return "redirect:/user/findAll.do";
}
}
10. 编写sql映射文件UserMapper.xml,Mybatis会根据该映射文件自动实现dao层的接口,实现数据库操作:
<?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.IUserInfoDao">
<select id="findAll" resultType="UserInfo">
select id, username, password
from userinfo
</select>
<insert id="insertAndGetId" keyProperty="id" parameterType="UserInfo">
insert into userinfo(username, password)
values (#{username}, #{password})
</insert>
<delete id="deleteById" parameterType="Integer">
delete
from userinfo
where id = #{id}
</delete>
<select id="selectById" parameterType="Integer" resultType="UserInfo">
select id, username, password
from userinfo
where id = #{id}
</select>
<update id="updateById" parameterType="UserInfo">
update userinfo
set username=#{username},
password=#{password}
where id = #{id}
</update>
</mapper>
11. 配置项目Tomcat服务器并运行,测试功能:
12. 运行结果
点击更改信息:
跳转至信息更改页面,并点击提交按钮:
更改结果: