ssm整合
整合思路:
1、搭建项目
2、导入jar包
3、引入配置文件(Spring配置文件,SpringMVC配置文件,myBatis配置文件,数据源文件,日志记录文件)
4、进行配置文件的配置
- 配置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>/*</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>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>springmvc</servlet-name>
<url-pattern>/</url-pattern>
</servlet-mapping>
- 配置数据源文件db.properties
driver=com.mysql.jdbc.Driver
url=jdbc:mysql://localhost:3306/demo
username=root
password=123456
- 配置mybatis配置文件
<configuration>
<!--类型别名 -->
<typeAliases>
<package name="com.bdqn.cn.pojo" />
</typeAliases>
</configuration>
- 配置Spring配置文件
<!-- 引入外部数据源配置文件 -->
<context:property-placeholder location="db.properties" />
<!-- 1、配置数据源 -->
<bean id="dataSource"
class="org.springframework.jdbc.datasource.DriverManagerDataSource">
<!--
数据库驱动
${driver}: ${}表示EL表达式 作用:直接获取数据
-->
<property name="driverClassName" value="${driver}" />
<!-- 链接数据库的url -->
<property name="url" value="${url}" />
<!-- 链接数据库的用户名 -->
<property name="username" value="${username}" />
<!-- 链接数据库的密码 -->
<property name="password" value="${password}" />
</bean>
<!-- 配置事务管理器,依赖于数据源 -->
<bean id="transactionManager"
class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
<property name="dataSource" ref="dataSource" />
</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 id="customerMapper" class="org.mybatis.spring.mapper.MapperScannerConfigurer">
<property name="basepackage" value="com.bdqn.cn.dao" />
</bean>
<!-- 扫描service包 -->
<context:component-scan base-package="com.bdqn.cn.service" />
- 配置SpringMVC配置文件
<!-- 指定需要扫描的包 -->
<context:component-scan base-package="com.bdqn.cn.controller" />
<!-- 开启注解驱动 -->
<mvc:annotation-driven />
<!-- 视图解析器 -->
<bean id="viewResolve"
class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<!-- 设置一个前缀 -->
<property name="prefix" value="/WEB-INF/jsp/" />
<!-- 设置一个后缀 -->
<property name="suffix" value=".jsp" />
</bean>
- 实例代码:
编写pojo类:
public class Customer {
private Integer id;
private String username;
private String jobs;
private String phone;
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;
}
public String getJobs() {
return jobs;
}
public void setJobs(String jobs) {
this.jobs = jobs;
}
public String getPhone() {
return phone;
}
public void setPhone(String phone) {
this.phone = phone;
}
}
编写dao接口和mapper文件:
dao 接口
public interface CustomerMapper {
public Customer findCustomerById(Integer id);
}
mapper 文件:
<mapper namespace="com.bdqn.cn.dao.CustomerMapper">
<select id="findCustomerById" parameterType="Integer" resultType="customer">
select * from Customer where id=#{id}
</select>
</mapper>
编写service接口和实现类:
public interface CustomerService {
public Customer findCustomerById(Integer id);
}
实现类:
@Service
public class CustomerServiceImpl implements CustomerService {
@Autowired
private CustomerMapper cm;
@Override
public Customer findCustomerById(Integer id) {
// TODO Auto-generated method stub
return this.cm.findCustomerById(id);
}
}
编写Controller类:
@Service
public class CustomerServiceImpl implements CustomerService {
@Autowired
private CustomerMapper cm;
@Override
public Customer findCustomerById(Integer id) {
// TODO Auto-generated method stub
return this.cm.findCustomerById(id);
}
}
运行结果: