SSM框架整合步骤

1如何进行SSM框架整合?

由于Spring MVC是Spring框架中的一个模块,所以Spring
MVC与Spring之间不存在整合的问题,只要引入相应JAR包就可以直接使用。因此SSM框架的整合就只涉及到了Spring与MyBatis的整合,以及Spring
MVC与MyBatis的整合。

如果想按照博客中的笔记配置,那么首先要明白一点,本次配置被划分为了两步骤.第一步骤是用来配置spring与mybastis. 第二步是配置spring与springmvc

首先来配置:spring与mybatis的整合

第一步:导入mybatis,druid数据源依赖(这里已经配置好了 springmvcy依赖)

<dependency>
  <groupId>javax.servlet</groupId>
  <artifactId>javax.servlet-api</artifactId>
  <version>3.0.1</version>
  <scope>provided</scope>
</dependency>
<dependency>
  <groupId>javax.servlet.jsp</groupId>
  <artifactId>jsp-api</artifactId>
  <version>2.1</version>
  <scope>provided</scope>
</dependency>

<!--  ioc 相关 -->
<dependency>
  <groupId>org.springframework</groupId>
  <artifactId>spring-context</artifactId>
  <version>${spirng.version}</version>
</dependency>

<!--aop 相关 -->
<dependency>
  <groupId>org.springframework</groupId>
  <artifactId>spring-aop</artifactId>
  <version>${spirng.version}</version>
</dependency>


<!-- aop**** aspectj 相关  -->
<dependency>
  <groupId>org.aspectj</groupId>
  <artifactId>aspectjrt</artifactId>
  <version>1.6.12</version>
</dependency>
<dependency>
  <groupId>org.aspectj</groupId>
  <artifactId>aspectjweaver</artifactId>
  <version>1.6.12</version>
</dependency>


<!-- springmvc 相关 -->
<dependency>
  <groupId>org.springframework</groupId>
  <artifactId>spring-webmvc</artifactId>
  <version>${spirng.version}</version>
</dependency>

<!--spring mvc-json依赖-->

<dependency>
  <groupId>com.fasterxml.jackson.core</groupId>
  <artifactId>jackson-databind</artifactId>
  <version>2.9.9</version>
</dependency>

<dependency>
  <groupId>com.fasterxml.jackson.core</groupId>
  <artifactId>jackson-core</artifactId>
  <version>2.9.9</version>
</dependency>

<dependency>
  <groupId>com.fasterxml.jackson.core</groupId>
  <artifactId>jackson-annotations</artifactId>
  <version>2.9.9</version>
</dependency>
<dependency>
  <groupId>com.alibaba</groupId>
  <artifactId>druid</artifactId>
  <version>1.1.16</version>
</dependency>


<!--mybatis-spring 依赖-->
<dependency>
  <groupId>org.mybatis</groupId>
  <artifactId>mybatis-spring</artifactId>
  <version>1.3.0</version>
</dependency>



<!--mybatis 依赖-->
<dependency>
  <groupId>org.mybatis</groupId>
  <artifactId>mybatis</artifactId>
  <version>3.4.5</version>
</dependency>

<dependency>
  <groupId>mysql</groupId>
  <artifactId>mysql-connector-java</artifactId>
  <version>5.1.6</version>
</dependency>

<dependency>
  <groupId>log4j</groupId>
  <artifactId>log4j</artifactId>
  <version>1.2.12</version>
</dependency>

<dependency>
  <groupId>org.springframework</groupId>
  <artifactId>spring-tx</artifactId>
  <version>${spirng.version}</version>
</dependency>
<dependency>
  <groupId>org.springframework</groupId>
  <artifactId>spring-jdbc</artifactId>
  <version>5.0.16.RELEASE</version>
</dependency>

2.将mybatis.properties,log4j.properties,mybatis-config.xml拷贝到resource文件下

mybatis.properties

driver=com.mysql.jdbc.Driver
url=jdbc:mysql://localhost:3306/student_db
name=root
password=123456

log4j.properties

# Set root category priority to INFO and its only appender to CONSOLE.
#log4j.rootCategory=INFO, CONSOLE            debug   info   warn error fatal
log4j.rootCategory=debug, CONSOLE, LOGFILE

# Set the enterprise logger category to FATAL and its only appender to CONSOLE.
log4j.logger.org.apache.axis.enterprise=FATAL, CONSOLE

# CONSOLE is set to be a ConsoleAppender using a PatternLayout.
log4j.appender.CONSOLE=org.apache.log4j.ConsoleAppender
log4j.appender.CONSOLE.layout=org.apache.log4j.PatternLayout
log4j.appender.CONSOLE.layout.ConversionPattern=%d{ISO8601} %-6r [%15.15t] %-5p %30.30c %x - %m\n

# LOGFILE is set to be a File appender using a PatternLayout.
log4j.appender.LOGFILE=org.apache.log4j.FileAppender
log4j.appender.LOGFILE.File=mybatis.log
log4j.appender.LOGFILE.Append=true
log4j.appender.LOGFILE.layout=org.apache.log4j.PatternLayout
log4j.appender.LOGFILE.layout.ConversionPattern=%d{ISO8601} %-6r [%15.15t] %-5p %30.30c %x - %m\n

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>

    <settings>
        <!-- 在这里设置日志,则日志打印在控制态,否则按照配置打印文件中 -->
        <setting name="logImpl" value="STDOUT_LOGGING" />
    </settings>

    <typeAliases>

        <package name="com.aaa.springmvc.entity"/>

    </typeAliases>

</configuration>

3.创建spring配置文件 applicationContext.xml


  <!--配置mybatis位置-->
    <context:property-placeholder location="classpath:mybatis.properties"/>

 <!-- 创建数据源 -->
    <bean id="dataSource" class="com.alibaba.druid.pool.DruidDataSource">

            <property name="driverClassName" value="${driver}"></property>
            <property name="url" value="${url}"/>
            <!--如果使用username 可以能和修通冲突-->
            <property name="username" value="${name}"/>
            <property name="password" value="${password}"/>
    </bean>
    
        <!--  设置 sqlSessionFactory -->
    <bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
            <property name="dataSource" ref="dataSource"/>
            <property name="configLocation" value="classpath:mybatis-config.xml"/>

    </bean>

    <!--设置扫描器-->
    <bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
        <!--配置包扫描器-->
        <property name="basePackage" value="com.aaa.springmvc.dao"/>
        <property name="sqlSessionFactoryBeanName" value="sqlSessionFactory"/>

    </bean>
    
    
    <!-- 开启扫描 -->
    <context:component-scan base-package="com.aaa.springmvc.service" />

    

在这里插入图片描述

这一步之前是把spring与mybatis整合到了一块,接下来这一步是把spring与spirngmvc整合到一块

4.创建mapper文件和接口

StudentDao

@Repository
public interface StudentDao {
    @Select(" select * from student where id = #{id} ")
    Student findStudentById(int id);
    @Update(" update student set name=#{name},age=#{age},sex=#{sex},height=#{height} where id = #{id} ")
    int updateStudent(Student student);
}

StudentServiceImpl

@Service
public class StudentServiceImpl implements StudentService {

    @Autowired
    private StudentDao studentDao;

    @Override
    public Student findStudentById(int id) {
        return studentDao.findStudentById(id);
    }

    @Override
    public int updateStudent(Student student) {

        return studentDao.updateStudent(student);
    }
}

SpringTest

 public static void main(String[] args) {
        ApplicationContext applicationContext = new ClassPathXmlApplicationContext("applicationContext.xml");
        StudentService studentService = applicationContext.getBean(StudentService.class);
        Student student=  studentService.findStudentById(5);
        System.out.println(student);
    }

spirng与springmvc绑定

src/main/webapp/WEB-INF/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>

com.aaa.springmvc.controller.StudentController

@Controller
public class StudentController {
    @Autowired
    private StudentService studentService;
    /**
     * 返回json 数据
     * @param id
     * @return
     */
    @RequestMapping("/findStudentById3")// 默认返回三种类型:1.String(jsp 路径) 2.ModelAndView 3.void
    @ResponseBody // MappingJackson2HttpMessageConverter 将Student 转换为 json str 返回给前端
    public Student findStudentById3(int id){

        return studentService.findStudentById(id);
    }
}

在这里插入图片描述
在这里插入图片描述

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值