标签:for 图片 静态 文件 http list odi ons nal
1.项目是maven,下面是pom.xml
1.先集成spring
<!-- 集成Spring --> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-core</artifactId> <version>5.1.4.RELEASE</version> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-context</artifactId> <version>5.1.4.RELEASE</version> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-context-support</artifactId> <version>5.1.4.RELEASE</version> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-aop</artifactId> <version>5.1.4.RELEASE</version> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-aspects</artifactId> <version>5.1.4.RELEASE</version> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-tx</artifactId> <version>5.1.4.RELEASE</version> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-jdbc</artifactId> <version>5.1.4.RELEASE</version> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-web</artifactId> <version>5.1.4.RELEASE</version> </dependency>
2.集成MVC
<!-- 集成SpringMvc --> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-webmvc</artifactId> <version>5.1.4.RELEASE</version> </dependency>
3.集成mybatis
<dependency> <groupId>org.mybatis</groupId> <artifactId>mybatis</artifactId> <version>3.5.0</version> </dependency>
4.集成mybatis-spring
<dependency> <groupId>org.mybatis</groupId> <artifactId>mybatis-spring</artifactId> <version>2.0.0</version> </dependency>
5.集成c3p0
<dependency> <groupId>com.mchange</groupId> <artifactId>c3p0</artifactId> <version>0.9.5.2</version> </dependency>
6.集成jstl
<dependency> <groupId>javax.servlet.jsp.jstl</groupId> <artifactId>jstl-api</artifactId> <version>1.2</version> </dependency> <dependency> <groupId>org.apache.taglibs</groupId> <artifactId>taglibs-standard-impl</artifactId> <version>1.2.5</version> </dependency>
7.集成mysql驱动
<dependency> <groupId>mysql</groupId> <artifactId>mysql-connector-java</artifactId> <version>8.0.14</version> </dependency>
2.按下图搭建项目框架
1.首先是domain.User代码
public class User implements Serializable { private Integer id; private String username; private String loginname; private String password; private Integer status; private Date createDate; public User() { super(); } public Integer getId() { return id; } public void setId(Integer id) { this.id = id; } public String getUsername() { return username; } public void setUsername(String username) { this.username = username; } public String getLoginname() { return loginname; } public void setLoginname(String loginname) { this.loginname = loginname; } public String getPassword() { return password; } public void setPassword(String password) { this.password = password; } public Integer getStatus() { return status; } public void setStatus(Integer status) { this.status = status; } public Date getCreateDate() { return createDate; } public void setCreateDate(Date createDate) { this.createDate = createDate; } @Override public String toString() { return "User [id=" + id + ", username=" + username + ", loginname=" + loginname + ", password=" + password + ", status=" + status + ", createDate=" + createDate + "]"; } }
2.数据协议层
public interface UserDao { public User selectByLoginnameAndPassword(); public User selectById(Integer id); public void deleteById(Integer id); public void update(User user); public List<User> selectByPage(); public Integer count(Map<String, Object> params); public void save(User user); public User selectByLoginnameAndPassword(String loginname, String password); }
3.服务协议层
public interface UserService { User login(String loginname,String password); User findUserById(Integer id); //List<User> findUser(User user,PageModel pageModel); void removeUserById(Integer id); void modifyUser(User user); void addUser(User user); }
4.服务层
public class UserServiceImpl implements UserService { @Autowired private UserDao userDao; public User login(String loginname, String password) { return userDao.selectByLoginnameAndPassword(loginname, password); } public User findUserById(Integer id) { return userDao.selectById(id); } public void removeUserById(Integer id) { userDao.deleteById(id); } public void modifyUser(User user) { userDao.update(user); } public void addUser(User user) { userDao.save(user); } }
5.然后在web.xml中配置spring
<!-- 配置spring核心监听器 --> <listener> <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class> </listener> <!-- contextConfigLocation参数用来指定Spring的配置文件 --> <context-param> <param-name>contextConfigLocation</param-name> <param-value>classpath:applicationContext*.xml</param-value>
</context-param>
6.在web.xml中配置SpringMvc
<!-- 定义Spring MVC的前端控制器 --> <servlet> <servlet-name>springmvc</servlet-name> <servlet-class> org.springframework.web.servlet.DispatcherServlet </servlet-class> <init-param> <param-name>contextConfigLocation</param-name> <param-value>classpath:springmvc-config.xml</param-value> </init-param> <load-on-startup>1</load-on-startup> </servlet> <!-- 让Spring MVC的前端控制器拦截所有请求 --> <servlet-mapping> <servlet-name>springmvc</servlet-name> <url-pattern>/</url-pattern> </servlet-mapping>
7.在web.xml中配置编码过滤器(post乱码)
<!-- 编码过滤器 --> <filter> <filter-name>characterEncodingFilter</filter-name> <filter-class>org.springframework.web.filter.CharacterEncodingFilter</filter-class> <init-param> <param-name>encoding</param-name> <param-value>UTF-8</param-value> </init-param> </filter> <filter-mapping> <filter-name>characterEncodingFilter</filter-name> <url-pattern>/*</url-pattern> </filter-mapping>
8.配置applicationContext.xml
1. mybatis:scan会扫描com.yongxing.manager.dao包里的所有接口当作Spring的bean配置,之后可以进行依赖注入
<mybatis:scan base-package="com.yongxing.manager.dao"/>
2.扫描com.yongxing.manager包下面的java文件,有Spring的相关注解的类,则把这些类注册为Spring的bean
<context:component-scan base-package="com.yongxing.manager"/>
3.加载数据源参数
<context:property-placeholder location="classpath:db.properties"/>
4.配置数据源
<bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource"> <property name="driverClass" value="${jdbc.driverClass}"></property> <property name="jdbcUrl" value="${jdbc.url}"></property> <property name="user" value="${jdbc.username}"></property> <property name="password" value="${jdbc.password}"></property> </bean>
5.配置SqlSessionFactory
<bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean"> <property name="dataSource" ref="dataSource" /> <!-- 自动扫描mapping.xml文件 --> <property name="mapperLocations" value="classpath:mapper/*.xml"></property> </bean>
9.置SpringMvc
1.自动扫描该包,SpringMVC会将包下用了@controller注解的类注册为Spring的controller
<context:component-scan base-package="com.yongxing.manager.controller"/>
2.设置默认配置方案
<mvc:annotation-driven/>
3.使用默认的Servlet来响应静态文件
<mvc:default-servlet-handler/>
4.视图解析器
<bean class="org.springframework.web.servlet.view.InternalResourceViewResolver"> <property name="prefix" value="/"/> <property name="suffix" value=".jsp"/> </bean>
10.配置Mapper接口
<?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.yongxing.manager.dao.UserDao"> <select id="selectByPage" resultType="com.yongxing.manager.domain.User" > select * from user_inf </select> </mapper>
11.配置登录页控制器
@Controller public class LoginController { @RequestMapping("/login") public ModelAndView login(ModelAndView mv) { System.out.println("login"); mv.setViewName("login"); return mv;}}
12.配置查询页控制器
@Controller public class UserController { @Autowired private UserDao userDao; @RequestMapping(value = "/all") public String del() { List<User> list = userDao.selectByPage(); for (int x = 0; x < list.size(); x++) { User user = list.get(x); System.out.println(user.getUsername()); } return null; } }
未完待续。。。。
标签:for 图片 静态 文件 http list odi ons nal
原文地址:https://www.cnblogs.com/cainame/p/10327312.html