(Spring+SpringMVC+MyBatis)实习第三天——ssm框架的搭建

今天学习了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. 运行结果

点击更改信息:

 跳转至信息更改页面,并点击提交按钮:

 

 更改结果:

 

 

  • 0
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值