为了账号安全,请及时绑定邮箱和手机立即绑定

我的学习记录——ssm整合1

标签:
Java

ssm框架整合
dao层:spring和mybatis整合,由spring来管理mapper接口
service层:spring管理接口和对象
web层:springMVC调用service层接口

1.spring和mabatis整合
(1)创建SqlMapConfig.xml配置文件
(2)创建applicationContext-dao.xml配置文件

    <!-- 加载数据库配置文件 -->
    <context:property-placeholder location="classpath:db.properties"/>

    <!-- 采用c3p0连接池 -->
    <bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource">
        <property name="driverClass" value="${jdbc.driver}"></property>
        <property name="jdbcUrl" value="${jdbc.url}"></property>
        <property name="user" value="${jdbc.username}"></property>
        <property name="password" value="${jdbc.password}"></property>
    </bean>

    <!-- 创建sqlSessionFactory -->
    <bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
        <property name="dataSource" ref="dataSource"></property>
        <!-- 配置mybatis核心配置文件地址 -->
        <property name="configLocation" value="classpath:mybatis/SqlMapConfig.xml"></property>
    </bean>

    <!-- 开启mapper扫描,在此处开启扫描mapper,在SqlMapConfig.xml中就不用配置mapper -->
    <bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
        <property name="basePackage" value="com.melon.mapper"></property>
        <property name="sqlSessionFactoryBeanName" value="sqlSessionFactory"></property>
    </bean>

(3)编写dao层接口和mapper

public interface UserMapper {
    public void register(User user) throws Exception;

    public List<User> searchUser(User user) throws Exception;
}

<mapper namespace="com.melon.mapper.UserMapper">  

    <sql id="searchUser">
        <if test="id != null">
            id = #{id}
        </if>
        <if test="username != null">
            and username = #{username}
        </if>
        <if test="address != null">
            and address = #{address}
        </if>
    </sql>

    <insert id="register" parameterType="user">
        insert into userlist (username,password,address) values (#{username},#{password},#{address})
    </insert>

    <select id="searchUser" parameterType="user" resultType="user">
        select * from userlist
        <where>
            <include refid="searchUser"></include>
        </where>
    </select>

</mapper>

2.spring与service层整合,service层调用mapper接口方法,创建applicationContext-service.xml文件,创建service对象
3.创建事务管理器,创建applicationContext-transaction.xml文件

    <bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
        <property name="dataSource" ref="dataSource"></property>
    </bean>

    <!-- 开启注解事务管理器 -->
    <tx:annotation-driven transaction-manager="transactionManager"/>

4.在web层使用springMVC
(1)创建springmvc.xml文件

<!-- 自动加载注解处理器映射器和处理器适配器 -->
<mvc:annotation-driven></mvc:annotation-driven>

<!-- 开启注解扫描 -->
<context:component-scan base-package="com.melon.controller"></context:component-scan>

<!-- 视图管理器 -->
<bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
    <!-- 设置返回页面的前缀和后缀 -->
    <property name="prefix" value="/WEB-INF/jsp/"></property>
    <property name="suffix" value=".jsp"></property>
</bean>
(2)在web.xml中配置前端控制器
  <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:spring/springmvc.xml</param-value>
    </init-param>
  </servlet>
  <servlet-mapping>
    <servlet-name>springMVC</servlet-name>
    <url-pattern>*.action</url-pattern>
  </servlet-mapping>

5.编写Controller

//@Controller注解表明这是一个Controller
@Controller
//此注解表示这个Controller的根路径,若要访问Register需要在前面加上/userService
@RequestMapping("/userService")
public class UserServiceHandler {

    @Autowired
    private UserService userService;

    //RequestMapping配置此控制器的访问路径
    @RequestMapping("/register")
    public String Register() throws Exception {
        User user = new User();
        user.setUsername("simon");
        user.setPassword("123");
        user.setAddress("龙江");

        userService.register(user);
        return "index";
    }

    @RequestMapping("/searchUser")
    public ModelAndView searchUser() throws Exception {
        User user = new User();
        user.setId(2);

        List<User> userList = userService.searchUser(user);
        ModelAndView modelAndView = new ModelAndView();
        modelAndView.addObject("userList", userList);
        modelAndView.setViewName("userList");
        return modelAndView;
    }
}

6.在web.xml中加载所有spring配置文件

  <context-param>
    <param-name>contextConfigLocation</param-name>
    <param-value>/WEB-INF/classes/spring/applicationContext-*.xml</param-value>
  </context-param>
  <listener>
    <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
  </listener>
点击查看更多内容
2人点赞

若觉得本文不错,就分享一下吧!

评论

作者其他优质文章

正在加载中
感谢您的支持,我会继续努力的~
扫码打赏,你说多少就多少
赞赏金额会直接到老师账户
支付方式
打开微信扫一扫,即可进行扫码打赏哦
今天注册有机会得

100积分直接送

付费专栏免费学

大额优惠券免费领

立即参与 放弃机会
意见反馈 帮助中心 APP下载
官方微信

举报

0/150
提交
取消