一、项目的创建
创建项目的图解:
创建Maven
通过以上的动作对项目进行创建。
二、项目文件夹的创建:
三、类的实现
(1)实体类UserInfo
package com.zhongruan.bean; public class UserInfo { private int id; private String username; private String password; public UserInfo() { } public int getId() { return id; } public String getUsername() { return username; } public String getPassword() { return password; } public void setId(int id) { this.id = id; } public void setUsername(String username) { this.username = username; } public void setPassword(String password) { this.password = password; } @Override public String toString() { return "UserInfo{" + "id=" + id + ", username='" + username + '\'' + ", password='" + password + '\'' + '}'; } }
(2)控制类UserInfoController
package com.zhongruan.controller; import com.zhongruan.bean.UserInfo; import com.zhongruan.dao.IUserInfoDao; 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 UserInfoController { @Autowired IUserInfoDao userInfoDao; @RequestMapping("findAll.do") public ModelAndView findAll(){ List<UserInfo> users=userInfoDao.findAll(); ModelAndView mv=new ModelAndView(); mv.addObject("users",users); mv.setViewName("allUser"); return mv; } }
(3)持久层IUserInfoDao
接口IUserInfoDao
package com.zhongruan.dao; import com.zhongruan.bean.UserInfo; import java.util.List; public interface IUserInfoDao { public List<UserInfo> findAll(); }
(4)使用mapper对IUserInfoDao接口进行实现
<?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="com.zhongruan.bean.UserInfo"> select * from userinfo; </select> </mapper>
(5)业务层Service
接口类
package com.zhongruan.service; import com.zhongruan.bean.UserInfo; import java.util.List; public interface IUserInfoService { public List<UserInfo> findAll(); }
实现
package com.zhongruan.service.Impl; import com.zhongruan.bean.UserInfo; import com.zhongruan.dao.IUserInfoDao; import com.zhongruan.service.IUserInfoService; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Service; import java.util.List; @Service("userInfoService") public class UserInfoServiceImpl implements IUserInfoService { @Autowired IUserInfoDao userInfoDao; @Override public List<UserInfo> findAll() { return userInfoDao.findAll(); } }
后面的xml以及其他的老师后面讲,以后再写。