SSM项⽬整合之简单⼊门案例
我们熟悉的SSM项⽬其实不是很神秘:Spring+SpringMVC+Mybatis
Spring帮助我们管理项⽬运⾏的类对象,SpringMVC对⽤户页⾯访问请求的处理⽽Mybatis则是和数据库交互帮助我们查获取数据,所以当我们整合起来的时候就是⼀个完整的项⽬了。
SSM项⽬整合⼊门案例:⽤户登录
第⼀步:清楚整合原理
Spring整合Mybatis,实现service和dao层的开发
SpringMVC整合Spring 实现controller层和service层开发
这样当控制器接受前端请求,直接使⽤逻辑业务层代码处理逻辑,然后返回对应的处理就⾏了
第⼆步:创建项⽬,添加依赖(和之前⼀样,创建java项⽬,后期修改成web项⽬)
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="/POM/4.0.0"
xsi="/2001/XMLSchema-instance"
schemaLocation="/POM/4.0.0 /xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>com.offcn</groupId>
<artifactId>SSMDemo001</artifactId>
<version>1.0-SNAPSHOT</version>
<properties>
<!--统⼀版本管理-->
<spring.version>5.1.5.RELEASE</spring.version>
</properties>
<dependencies>
<!-- springwebmvc  -->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-webmvc</artifactId>
<version>${spring.version}</version>
</dependency>
<!-- Spring 整合Jdbc -->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-jdbc</artifactId>
<version>${spring.version}</version>
</dependency>
<!--Spring-test spring测试包 -->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-test</artifactId>
<version>${spring.version}</version>mvc的controller
</dependency>
<!-- Spring⾯向切⾯编程 -->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-aspects</artifactId>
<version>${spring.version}</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context-support</artifactId>
<version>${spring.version}</version>
<!-- 返回json字符串的⽀持  -->
<dependency>
<groupId>com.</groupId>
<artifactId>jackson-databind</artifactId>
<version>2.9.9</version>
</dependency>
<!--  处理⽂件上传 -->
<dependency>
<groupId>commons-fileupload</groupId>
<artifactId>commons-fileupload</artifactId>
<version>1.3.1</version>
</dependency>
<dependency>
<groupId>commons-io</groupId>
<artifactId>commons-io</artifactId>
<version>2.4</version>
</dependency>
<!--MyBatis -->
<dependency>
<groupId&batis</groupId>
<artifactId>mybatis</artifactId>
<version>3.4.2</version>
</dependency>
<!-- MyBatis整合Spring的适配包 -->
<dependency>
<groupId&batis</groupId>
<artifactId>mybatis-spring</artifactId>
<version>1.3.1</version>
</dependency>
<!--引⼊pageHelper分页插件  PageInfo -->
<dependency>
<groupId>com.github.pagehelper</groupId>
<artifactId>pagehelper</artifactId>
<version>5.0.0</version>
</dependency>
<!-- MySQL驱动包 -->
<!-- mvnrepository/artifact/mysql/mysql-connector-java --> <dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>5.1.47</version>
</dependency>
<!-- 阿⾥巴巴数据源 druid -->
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>druid</artifactId>
<version>1.1.10</version>
</dependency>
<!-- jstl jsp标签库包 -->
<dependency>
<groupId>jstl</groupId>
<artifactId>jstl</artifactId>
<version>1.2</version>
</dependency>
<!-- servlet api包 -->
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>javax.servlet-api</artifactId>
<version>3.0.1</version>
<scope>provided</scope>
</dependency>
<!-- junit 测试包 -->
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.12</version>
</dependency>
<!-- ⽇志包 -->
<dependency>
<groupId>log4j</groupId>
<artifactId>log4j</artifactId>
<version>1.2.16</version>
</dependency>
<!--c3p0 连接池 -->
<dependency>
<groupId>c3p0</groupId>
<artifactId>c3p0</artifactId>
<version>0.9.1.2</version>
</dependency>
</dependencies>
<!--读取Java⽬录下的xml⽂件-->
<build>
<resources>
<resource>
<directory>src/main/java</directory>
<includes>
<include>**/*.xml</include>
</includes>
</resource>
</resources>
</build>
</project>
第三步:划分项⽬结构:
第四步:书写前端页⾯,这⾥有能⼒的可以写精美⼀些的
第五步:配置容器和l:
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="/schema/beans"
xsi="/2001/XMLSchema-instance"
context="/schema/context"
mvc="/schema/mvc"
schemaLocation="/schema/beans
/schema/beans/spring-beans.xsd
/schema/context
/schema/context/spring-context.xsd
/schema/mvc
/schema/mvc/spring-mvc.xsd">
<!--扫描哪些包,这⾥mvc扫描的是控制器在的包-->
<component-scan base-package="ller"></component-scan> <!--开启springmvc注解操作-->
<annotation-driven></annotation-driven>
<!--开启视图解析器-->
<bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="prefix"value="/"></property>
<property name="suffix"value=".jsp"></property>
</bean>
</beans>
<?xml version="1.0" encoding="UTF-8"?>
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="/schema/beans"
xsi="/2001/XMLSchema-instance"
context="/schema/context"
context="/schema/context"
aop="/schema/aop"
schemaLocation="/schema/beans
/schema/beans/spring-beans.xsd
/schema/context
/schema/context/spring-context.xsd
/schema/tx
/schema/tx/spring-tx.xsd
/schema/aop
/schema/aop/spring-aop.xsd">
<!--开启注解模式-->
<annotation-config></annotation-config>
<!--注解扫描包-->
<component-scan base-package="com.offcn"></component-scan>
<!-- 引⼊数据库配置 -->
<property-placeholder location="classpath:db.properties"/>
<!-- 数据库连接池 -->
<bean id="ds"class="com.alibaba.druid.pool.DruidDataSource"destroy-method="close">
<!-- 数据库基本信息配置 -->
<property name="url"value="${url}"/>
<property name="username"value="${jdbc_username}"/>
<property name="password"value="${jdbc_password}"/>
<property name="driverClassName"value="${driverClassName}"/>
<property name="filters"value="${filters}"/>
<!-- 最⼤并发连接数 -->
<property name="maxActive"value="${maxActive}"/>
<!-- 初始化连接数量 -->
<property name="initialSize"value="${initialSize}"/>
<!-- 配置获取连接等待超时的时间 -->
<property name="maxWait"value="${maxWait}"/>
<!-- 最⼩空闲连接数 -->
<property name="minIdle"value="${minIdle}"/>
<!-- 配置⼀个连接在池中最⼩⽣存的时间,单位是毫秒 -->
<property name="minEvictableIdleTimeMillis"value="${minEvictableIdleTimeMillis}"/>
<property name="validationQuery"value="${validationQuery}"/>
<property name="testWhileIdle"value="${testWhileIdle}"/>
<property name="testOnBorrow"value="${testOnBorrow}"/>
<property name="testOnReturn"value="${testOnReturn}"/>
<!-- 打开removeAbandoned功能 -->
<property name="removeAbandoned"value="${removeAbandoned}"/>
<!-- 1800秒,也就是30分钟 -->
<!-- 关闭abanded连接时输出错误⽇志 -->
<property name="logAbandoned"value="${logAbandoned}"/>
</bean>
<!--配置SqlSessionFactoryBean:MyBatis整合SpringAPI类-->
<bean id="sqlSessionFactory"class="batis.spring.SqlSessionFactoryBean">
<property name="dataSource"ref="ds"></property>
<property name="typeAliasesPackage"value="com.offcn.pojo"></property>
<property name="mapperLocations"value="classpath:com/offcn/mapper/*l"></property> <!-- 加载mybatis的配置⽂件 -->
<property name="configLocation"value="l"/>
<!-- mybatis 提供的分页插件的配置引⼊pagehelper分页插件 -->
<property name="plugins">
<array>

版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系QQ:729038198,我们将在24小时内删除。