Spring004

第一步:编写UserMapper接口 和UserMapper.xml文件

package com.zll.dao;

import com.zll.pojo.User;
import org.apache.ibatis.annotations.Param;

/**
 * @auther zll
 * @create 2020/7/20-16:01
 */
public interface UserMapper {
    //根据用户id修改信息
    public int updateUser(@Param("id") int id, @Param("u") User user);

    //根据id删除用户
    public int deleteUser(@Param("id")int id);

    //根据id、修改密码
    public int updatePwdById(@Param("id") int id,@Param("password")String pwd);
}
<?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="com.zll.dao.UserMapper">

    <update id="updateUser">
        update smbms_user set userName=#{u.userName} where id=#{id}
    </update>

    <delete id="deleteUser">
        delete from smbms_user where id=#{id}
    </delete>

    <update id="updatePwdById" >
        update smbms_user set userPassword=#{u.userPassword} where id=#{id}
    </update>
</mapper>

 第二步,编写接口的实现类UserMapperImpl

import org.springframework.stereotype.Repository;

/**
 * @auther zll
 * @create 2020/7/20-16:02
 */

public class UserMapperImpl implements UserMapper {

    private SqlSessionTemplate sqlSessionTemplate;

    public SqlSessionTemplate getSqlSessionTemplate() {
        return sqlSessionTemplate;
    }

    public void setSqlSessionTemplate(SqlSessionTemplate sqlSessionTemplate) {
        this.sqlSessionTemplate = sqlSessionTemplate;
    }

    @Override
    public int updateUser(int id, User user) {
        return sqlSessionTemplate.getMapper(UserMapper.class).updateUser(id,user);
    }

    @Override
    public int deleteUser(int id) {
        return sqlSessionTemplate.getMapper(UserMapper.class).deleteUser(id);
    }

    @Override
    public int updatePwdById(int id, String pwd) {
        return sqlSessionTemplate.getMapper(UserMapper.class).updatePwdById(id,pwd);
    }
}

 第三步,编写UserService接口和其实现类UserServiceImpl

import com.zll.pojo.User;

/**
 * @auther zll
 * @create 2020/7/20-10:22
 */
public interface UserService {

    public boolean updateUser(int id, User user);

    public boolean delete(int id);

    public boolean updatePassword(int id, String pwd);
}

 

public class UserServiceImpl implements UserService {


    private UserMapper userMapper;

    public UserMapper getUserMapper() {
        return userMapper;
    }

    public void setUserMapper(UserMapper userMapper) {
        this.userMapper = userMapper;
    }

    @Override
    public boolean updateUser(int id, User user) {
        return userMapper.updateUser(id,user)>0;
    }

    @Override
    public boolean delete(int id) {
        return userMapper.deleteUser(id)>0;
    }

    @Override
    public boolean updatePassword(int id, String pwd) {
        return userMapper.updatePwdById(id,pwd)>0;
    }
}

 第四步:编写Spring的配置文件

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
       xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">
    <bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource">
        <property name="username" value="root"></property>
        <property name="password" value="123456"></property>
        <property name="url" value="jdbc:mysql:///smbms"></property>
        <property name="driverClassName" value="com.mysql.jdbc.Driver"></property>
    </bean>

    <bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
        <!--        配置数据源-->
        <property name="dataSource" ref="dataSource"></property>
        <!--        配置Mybatis-config文件-->
        <property name="configLocation" value="classpath:mybatis-config.xml"></property>
        <!--        配置映射文件-->
        <!--        <property name="mapperLocations" value="classpath:com/MyBatis/dao/ProviderMapper.xml"></property>-->
        <!--        -->
        <property name="mapperLocations" value="classpath:com/zll/dao/*.xml"></property>
    </bean>

    <bean id="sqlSessionTemplate" class="org.mybatis.spring.SqlSessionTemplate">
        <!--        构造注入sqlSessionFactory-->
        <constructor-arg ref="sqlSessionFactory"/>
    </bean>


<!--    <bean id="userMapper" class="com.zll.dao.impl.UserMapperImpl">-->
<!--        <property name="sqlSessionTemplate" ref="sqlSessionTemplate"></property>-->
<!--    </bean>-->

    <bean id="userMapperProxy" class="org.mybatis.spring.mapper.MapperFactoryBean">
        <!--配置要生成实现类的接口 -->
        <property name="mapperInterface" value="com.zll.dao.UserMapper"></property>
        <property name="sqlSessionFactory" ref="sqlSessionFactory"></property>
    </bean>

<!--    <bean id="userService" class="com.zll.service.impl.UserServiceImpl">-->
<!--        <property name="userMapper" ref="userMapper"></property>-->
<!--    </bean>-->

    <bean id="userService" class="com.zll.service.impl.UserServiceImpl">
        <property name="userMapper" ref="userMapperProxy"></property>
    </bean>

</beans>

 第五步:测试

@Test
    public void test1(){
        ApplicationContext context = new ClassPathXmlApplicationContext("applicationContext.xml");

        UserService userService = context.getBean("userService", UserService.class);

        User user = new User();
        user.setUserName("章子怡");
        userService.updateUser(20,user);
    }

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值