第一个ssm小项目

1.项目目录结构





2.xml的配置

web.xml

<!DOCTYPE web-app PUBLIC
        "-//Sun Microsystems, Inc.//DTD Web Application 2.3//EN"
        "http://java.sun.com/dtd/web-app_2_3.dtd" >

<web-app xmlns="http://xmlns.jcp.org/xml/ns/javaee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee
                      http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd"
         version="3.1" metadata-complete="true">
    <display-name>Archetype Created Web Application</display-name>
    <!--配置DispatcherServlet-->
    <servlet>
        <servlet-name>school</servlet-name>
        <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
        <!--初始化spring容器-->
        <init-param>
            <param-name>contextConfigLocation</param-name>
            <param-value>classpath:applicationContext.xml</param-value>
        </init-param>
    </servlet>
    <servlet-mapping>
        <servlet-name>school</servlet-name>
        <url-pattern>/</url-pattern>
    </servlet-mapping>
    <!-- Spring 的监听器可以通过这个上下文参数来获取school-mybatis.xml的位置
     1.启动一个WEB项目的时候,容器(如:Tomcat)会去读它的配置文件web.xml.读两个节点:
     <listener></listener> 和 <context-param></context-param>
     2.紧接着,容器创建一个ServletContext(上下文),这个WEB项目所有部分都将共享这个上下文.
     3.容器将<context-param></context-param>转化为键值对,并交给ServletContext.-->
    <!--<context-param>-->
        <!--<param-name>contextConfigLocation</param-name>-->
        <!--<param-value>classpath:applicationContext.xml</param-value>-->
    <!--</context-param>-->

    <!-- 创建Spring的监听器
      ContextLoaderListener监听器的作用就是启动Web容器时,自动装配ApplicationContext的配置信息。
      applicationContext.xml的文件位置就可以有两种默认实现:
      第一种:直接将之放到/WEB-INF下,之在web.xml中声明一个listener;
      第二种:将之放到classpath下,但是此时要在web.xml中加入<context-param>,
      用它来指明你的applicationContext.xml的位置以供web容器来加载。-->
    <!--<listener>-->
        <!--<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>-->
    <!--</listener>-->

</web-app>


applicationContext.xml

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
       xmlns:context="http://www.springframework.org/schema/context"
       xmlns:tx="http://www.springframework.org/schema/cache" xmlns:mvc="http://www.springframework.org/schema/mvc"
       xsi:schemaLocation="http://www.springframework.org/schema/beans
    http://www.springframework.org/schema/beans/spring-beans.xsd
    http://www.springframework.org/schema/context
    http://www.springframework.org/schema/context/spring-context.xsd http://www.springframework.org/schema/cache http://www.springframework.org/schema/cache/spring-cache.xsd http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc.xsd">
    <mvc:annotation-driven/>
    <context:component-scan base-package="cn.hwj"></context:component-scan>
   <!--dao层-->
    <bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource">
        <!-- 配置连接池属性 -->
        <property name="driverClass" value="com.mysql.jdbc.Driver"/>
        <property name="jdbcUrl" value="jdbc:mysql://127.0.0.1:3306/blog?useUnicode=true&characterEncoding=UTF-8"/>
        <property name="user" value="root"/>
        <property name="password" value="root"/>
        <!--看不懂这是什么!!大概是固定的吧!!-->
        <!-- c3p0连接池的私有属性 -->
        <property name="maxPoolSize" value="30"/>
        <property name="minPoolSize" value="10"/>
        <!-- 关闭连接后不自动commit -->
        <property name="autoCommitOnClose" value="false"/>
        <!-- 获取连接超时时间 -->
        <property name="checkoutTimeout" value="10000"/>
        <!-- 当获取连接失败重试次数 -->
        <property name="acquireRetryAttempts" value="2"/>
    </bean>

    <bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
        <property name="dataSource" ref="dataSource"></property>
        <!--mybatis配置文件的位置-->
        <property name="configLocation" value="classpath:mybatis-config.xml"></property>
        <!--扫描mapper.xml的位置-->
        <property name="mapperLocations" value="classpath:UserMapper.xml"></property>
    </bean>
    <!--???根据UserMapper接口生成实现类-->
    <bean id="userMapper" class="org.mybatis.spring.mapper.MapperFactoryBean">
        <property name="sqlSessionFactory" ref="sqlSessionFactory"></property>
        <property name="mapperInterface" value="cn.hwj.dao.UserMapper"></property>
    </bean>

   <!--web层-->
    <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
        <property name="prefix" value="/WEB-INF/view/"/>
        <property name="suffix" value=".jsp"/>
    </bean>
</beans>


mybatis.xml

<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE configuration
        PUBLIC "-//mybatis.org//DTD Config 3.0//EN"
        "http://mybatis.org/dtd/mybatis-3-config.dtd">
<configuration>
    <!--<mappers>-->
        <!--<mapper resource="UserMapper.xml"/>-->
    <!--</mappers>-->
</configuration>


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="cn.hwj.dao.UserMapper">
    <select id="selectAll" resultType="cn.hwj.domain.User">
       SELECT  * from users;
    </select>
</mapper>



3.User实体类

package cn.hwj.domain;

public class User {
    private int id;
    private String name;
    private int age;

    public int getId() {
        return id;
    }

    public void setId(int id) {
        this.id = id;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public int getAge() {
        return age;
    }

    public void setAge(int age) {
        this.age = age;
    }
}

4.dao层mapper接口类

package cn.hwj.dao;

import cn.hwj.domain.User;

import java.util.List;

public interface UserMapper {
    public  List<User> selectAll();
}

5.service层

1.接口类

package cn.hwj.service;

import cn.hwj.domain.User;

import java.util.List;

public interface UserService {
    public abstract List<User> selectAll();
}

2.实现类

package cn.hwj.service;

import cn.hwj.dao.UserMapper;
import cn.hwj.domain.User;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
@Service
public class UserServiceImpl implements UserService {
    @Autowired
    private UserMapper userMapper;
    public List<User> selectAll() {
        return userMapper.selectAll();
    }
}


6.web层controller类

package cn.hwj.web;

import cn.hwj.domain.User;
import cn.hwj.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;

import java.util.List;
@Controller
public class HomeController {
    @Autowired
    private UserService userService;
    @RequestMapping("/test")
    public String home(Model model){
        List<User> users=userService.selectAll();
        model.addAttribute("users",users);
         return "home";
    }
}

7.jsp

<%@ page language="java" contentType="text/html; charset=UTF-8"
         pageEncoding="UTF-8"%>
<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c"%>
<%@ page isELIgnored="false" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
    <title>Title</title>
</head>
<body>
<table>
    <tr>
        <td>ID</td>
        <td>NAME</td>
        <td>AGE</td>
    </tr>
    <c:forEach items="${users}" var="user">
        <tr>
            <td>${user.id}</td>
            <td>${user.name}</td>
            <td>${user.age}</td>
        </tr>
    </c:forEach>
</table>
测试
</body>
</html>

注意事项

1.UserMapper.xml返回一个user集合,resultType为user即可

2.在jsp页面使用jstl表达式的时候需要加入标签

<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c"%>





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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值