初步搭建ssm项目(idea工具)

一 建立一个WEB项目,然后就是一些jar包的引入,如下图

初步搭建ssm项目(idea工具)

二 除了以上基本的spring jar包,还需要mybatis和spring整合包,数据库连接池以及springmvc,如下图

   <dependency>
    <groupId>org.springframework</groupId>
    <artifactId>spring-webmvc</artifactId>
    <version>5.2.3.RELEASE</version>
</dependency>
    <dependency>
        <groupId>org.springframework</groupId>
        <artifactId>spring-web</artifactId>
        <version>5.2.3.RELEASE</version>
    </dependency>
    <dependency>
        <groupId>org.springframework</groupId>
        <artifactId>spring-jdbc</artifactId>
        <version>5.3.5</version>
    </dependency>
    <!-- https://mvnrepository.com/artifact/org.apache.commons/commons-dbcp2 -->
    <dependency>
        <groupId>org.apache.commons</groupId>
        <artifactId>commons-dbcp2</artifactId>
        <version>2.2.0</version>
    </dependency>
    <!-- https://mvnrepository.com/artifact/org.apache.commons/commons-pool2 -->
    <dependency>
        <groupId>org.apache.commons</groupId>
        <artifactId>commons-pool2</artifactId>
        <version>2.4.2</version>
    </dependency>
    <dependency>
        <groupId>org.mybatis</groupId>
        <artifactId>mybatis-spring</artifactId>
        <version>2.0.2</version>
    </dependency>

在资源包下建立数据库常量配置文件db.properties,spring配置文件applicationContext.xml,Mybatis配置文件mybatis-config.xml,以及springmvc配置文件springmvc-config.xml。
初步搭建ssm项目(idea工具)
1.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:aop="http://www.springframework.org/schema/aop"
       xmlns:context="http://www.springframework.org/schema/context"
       xmlns:tx="http://www.springframework.org/schema/tx"

       xsi:schemaLocation="http://www.springframework.org/schema/beans
        http://www.springframework.org/schema/beans/spring-beans.xsd
        http://www.springframework.org/schema/aop
        http://www.springframework.org/schema/aop/spring-aop.xsd
        http://www.springframework.org/schema/context
        http://www.springframework.org/schema/context/spring-context.xsd
        http://www.springframework.org/schema/tx
        http://www.springframework.org/schema/tx/spring-tx.xsd">
    <!--读取db.properties-->
    <context:property-placeholder location="classpath:db.properties"/>
    <!-- 配置数据源-->
    <bean id="dataSource" class="org.apache.commons.dbcp2.BasicDataSource">
        <!--      数据库驱动-->
        <property name="driverClassName" value="${jdbc.driver}"/>
        <property name="url"  value="${jdbc.url}"/>
        <property name="username" value="${jdbc.username}"/>
        <property name="password" value="${jdbc.password}"/>
        <property name="maxTotal" value="${jdbc.maxTotal}"/>
        <property name="maxIdle" value="${jdbc.maxIdle}"/>
        <property name="initialSize" value="${jdbc.initialSize}"/>

    </bean>
<!--    事务管理器-->
  <bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
      <property name="dataSource" ref="dataSource"></property>
  </bean>
    <!--注册事务管理器驱动,开启事务注解-->
    <tx:annotation-driven transaction-manager="transactionManager"/>
    <!--    配置mybatis工厂-->
    <bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
        <!--    注入数据源-->
        <property name="dataSource" ref="dataSource"/>
        <!--指定核心配置文件位置-->
        <property name="configLocation" value="classpath:mybatis-config.xml"/>
    </bean>
<!--配置mapper扫描器-->
    <bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
        <property name="basePackage" value="com.ssm.dao"></property>
    </bean>
<!--    扫描service-->
    <context:component-scan base-package="com.ssm.service"/>
</beans>

2.mybatis-config.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>
<!--配置别名-->
    <typeAliases>
        <package name="com.ssm.po"/>
    </typeAliases>
</configuration>

3.springmvc-config.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: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 https://www.springframework.org/schema/context/spring-context.xsd http://www.springframework.org/schema/mvc https://www.springframework.org/schema/mvc/spring-mvc.xsd
http://www.springframework.org/schema/context ">
<!--    指定需要扫描的包-->
    <context:component-scan base-package="com.ssm.controller"/>
<!--    配置注解驱动-->
    <mvc:annotation-driven/>
<!--    定义视图解析器-->
    <bean id="ViewResolver" class="org.springframework.web.servlet.view.InternalResourceViewResolver">
        <!--        设置前缀-->
        <property name="prefix" value="/jsp/"/>
        <!--        设置后缀-->
        <property name="suffix" value=".jsp"/>
    </bean>
</beans>

4.包结构如下
初步搭建ssm项目(idea工具)

public class User {
    private Integer id;
    private String username;

    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;
    }
```java
public interface UserDao {
    public User findUserById(Integer id);
}
public interface UserService {
    public User findUserById(Integer id);
}
//userdao的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.ssm.dao.UserDao">
    <select id="findUserById" parameterType="Integer" resultType="User">
        select  * from t_user where id=#{id}
    </select>

</mapper>





@Service
//标识类中所有方法纳入spring事务管理
@Transactional
public class UserServiceImp implements UserService {
    @Autowired
    private UserDao userDao;

    @Override
    public User findUserById(Integer id) {
        //查询客户
       return this.userDao.findUserById(id);
    }
}


@Controller
public class UserController {
    @Autowired
    private UserService userService;
    @RequestMapping("/findUserById")
    public  String findUserById(Integer id, Model model){
        User user=userService.findUserById(id);
        model.addAttribute("user",user);
        //返回用户信息展示页面
        return "user";
    }
}

5.web.xml的配置

!--    配置加载spring文件的监听器-->
    <context-param>
        <param-name>contextConfigLocation</param-name>
        <param-value>classpath:applicationContext.xml</param-value>
    </context-param>
    <listener>
        <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
    </listener>
<!--    编码过滤器-->
    <filter>
        <filter-name>encoding</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>encoding</filter-name>
        <url-pattern>*.action</url-pattern>
    </filter-mapping>
<!--    配置springmvc前端控制器-->
    <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>
<!--        配置服务器时立即加载SPRINGMVC文件-->
        <load-on-startup>1</load-on-startup>

    </servlet>
<servlet-mapping>
    <servlet-name>springmvc</servlet-name>
<!--    /拦截所有请求,jsp除外-->
    <url-pattern>/</url-pattern>
</servlet-mapping>
</web-app>

6.创建一个名为jsp的文件夹,创建一个user.jsp

<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
    <meta http-equiv="Content-Type" content="text/html;charset=UTF-8">
    <title>用户信息</title>
</head>
<body>
    <table>
        <tr>
            <td>用户ID</td>
            <td>${user.id}</td>
        </tr>
        <tr>
            <td>用户姓名</td>
            <td>${user.username}</td>
        </tr>
    </table>
</body>
</html>

7.发布到tomca服务器上结果如下
初步搭建ssm项目(idea工具)

三 总结可能会遇到的问题:

1.一个或多个listeners启动失败,web application 找不到此类,所以要在web下创建lib文件引入jar包 ,在IDEA中点击File > Project Structure > Artifacts > 在右侧Output Layout右击项目名,选择Put into Output Root。

​执行后,在WEB-INF在增加了lib目录,里面是项目引用的jar包,点击OK。
2.http500错误,由于mapper映射文件没有被扫描到。
此异常的原因是由于mapper接口编译后在同一个目录下没有找到mapper映射文件而出现的。由于maven工程在默认情况下src/main/java目录下的mapper文件是不发布到target目录下的。
在pom下写入



src/main/java

/*.properties
/*.xml

false


上一篇:java ssm+jsp 旅游网站的设计与实现【计算机毕业设计】


下一篇:Shiro授权-SSM