SSM三大框架搭建(Spring+Spring MVC+mybatis)

第一步导入SSM整合所需要的jar包

spring jar包+mybatis jar包+spring MVC jar包(spring-web包,spring-webMVC包)


1.2 导入配置文件

里面的其他配置信息是测试用的,在配置这心文件时,一定要细心,不然出问题了,就很难找到。同时在配置文件信息时,想想他们之间存在怎样的的关系,按着它们之间的依赖管关系来一一配置,这样还不容易出错

Spring核心配置文件applicationContext.xml

application.xml文件中的的配置文件主要是数据库的依赖和要扫描的包。

首先将数据库连接池文件映射到application.xml文件中,然后获取数据库连接池,然后数据库连接池有依赖与sqlSessionFactoryBean,获取DataSource后,mapper.xml文件里的sql语句又依赖与SQLSessionFactoryBean,而mapper.xml文件直接面向接口编程,所以不需要接口的实体类,最后获取sqlMapperConfig文件,又因为sqlMapperConfig.xml文件包裹mapper.xml可以直接获取sqlMapperConfig.xml文件,如果某个文件夹的文件需要加载,可以将该文件夹的目录添加到扫描器中,当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"
       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"
>

        <context:property-placeholder location="classpath:db.properties"></context:property-placeholder>
        <bean name="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource">
                <property name="password" value="${jdbc.password}"></property>
                <property name="user" value="${jdbc.username}"></property>
                <property name="jdbcUrl" value="${jdbc.url}"></property>
                <property name="driverClass" value="${jdbc.driver}"></property>
        </bean>

        <bean name="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
                <property name="dataSource" ref="dataSource"></property>
                <property name="configLocation" value="classpath:sqlMapConfig.xml"></property>
        </bean>

        <bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
                <property name="basePackage" value="cn.hd.mapper"></property>
        </bean>
        <context:component-scan base-package="cn.hd"></context:component-scan>
</beans>

mybatis配置文件sqlMapperConfig.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="cn.hd.entity"></package>
 </typeAliases>
<mappers>
    <package name="cn.hd.mapper"></package>
</mappers>


</configuration>

springmvc配置文件springmvc.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:p="http://www.springframework.org/schema/p"
       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-4.0.xsd
        http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-4.0.xsd
        http://www.springframework.org/schema/context
" >http://www.springframework.org/schema/context/spring-context-4.0.xsd">

<context:component-scan base-package="cn.hd.controller"></context:component-scan>


</beans>

log4j配置文件log4j.properties

# Global logging configuration
#在开发环境下日志级别设置为debug,生产环境下设置成infoerror
log4j.rootLogger=DEBUG, stdout
# Console output...
log4j.appender.stdout=org.apache.log4j.ConsoleAppender
log4j.appender.stdout.layout
=org.apache.log4j.PatternLayout
log4j.appender.stdout.layout.ConversionPattern
=%5p [%t] - %m%n

数据可连接池配置文件db.properties

jdbc.driver=com.mysql.jdbc.Driver
jdbc.url
=jdbc:mysql://localhost:3306/mybatis?useUnicode=true&characterEncoding=utf-8
jdbc.username
=root
jdbc.password
=1234

web.xml文件 配置
配置springMVC的web控制器以及路径以及访问的路
当项目启动时,加载applicationContext里的文件

<?xml version="1.0" encoding="UTF-8"?>
<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"
>

    <!--配置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.xml</param-value>
        </init-param>
    </servlet>
    <servlet-mapping>
        <servlet-name>springmvc</servlet-name>

        <!--  /*能拦截所有的资源包括jsp html css
        *.action   *.do 后缀名可以用

        目录拦截 会放行动态资源
        -->
        
<url-pattern>*.action</url-pattern>
    </servlet-mapping>

    <listener>
        <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
    </listener>
    <context-param>
        <param-name>contextConfigLocation</param-name>
        <param-value>classpath:applicationContext.xml</param-value>
    </context-param>
</web-app>


第二步 创建bean类

User类


public class User {
    private Integer id;
    private String name;
    private String sex;
    private String address;
    private Integer balance;

    public User() {
    }

    public User(Integer id, String name, String sex, String address, Integer balance) {
        this.id = id;
        this.name = name;
        this.sex = sex;
        this.address = address;
        this.balance = balance;
    }

    public Integer getId() {
        return id;
    }

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

    public String getName() {
        return name;
    }

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

    public String getSex() {
        return sex;
    }

    public void setSex(String sex) {
        this.sex = sex;
    }

    public String getAddress() {
        return address;
    }

    public void setAddress(String address) {
        this.address = address;
    }

    public Integer getBalance() {
        return balance;
    }

    public void setBalance(Integer balance) {
        this.balance = balance;
    }

    @Override
    public String toString() {
        return "User{" +
                "id=" + id +
                ", name='" + name + '\'' +
                ", sex='" + sex + '\'' +
                ", address='" + address + '\'' +
                ", balance=" + balance +
                '}';
    }
}

第四步创建controller类

UserController类,适用于web层


@Controller
public class UserController {

    @Resource(name = "userServiceImpl")
    private UserService userService;

    @RequestMapping("/login.action")
    public ModelAndView login(){
        User userById = userService.getUserById(1);
        System.out.println(userById);
        ModelAndView modelAndView = new ModelAndView();
        modelAndView.setViewName("/index.jsp");
        return modelAndView;
    }
}

第五步 创建service

UserService接口和UserServiceImpl实现类,适用于service层


public interface UserService {
    User getUserById(Integer id);
}

 


@Service
public class UserServiceImpl implements UserService {

    @Resource(name = "userMapper")
    private UserMapper userMapper;
    @Override
    public User getUserById(Integer id) {
        User userById = userMapper.findUserById(id);
        return userById;
    }
}

第六步 创建mapper

UserMapper接口和UserMapper.xml配置文件,适用于dao层,


@Repository
public interface UserMapper {
    User findUserById(Integer id);
}

 

<?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.hd.mapper.UserMapper">
    <!--<mapper namespace="test">-->

    
<select id="findUserById" parameterType="int" resultType="user">
        SELECT * FROM mbs WHERE id=#{id}
    </select>
</mapper>

第七部配置toncat,并启动

在启动前记得创建数据库

在浏览器中输入

http://localhost:8080/login.action

能够跳转到8080端口的页面,并且控制台能够打印输出UserMapper.xml文件里配置的查询语句


能输出查询的信息,说明你的ssm搭建成功了


  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

double_lifly

点喜欢就是最好的打赏!!

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值