Spring-mybatis实现curd--maven

Spring-mybatis实现curd--maven,第1张

1.pom.xml导入依赖
 <dependencies>
        <dependency>
            <groupId>junitgroupId>
            <artifactId>junitartifactId>
            <version>4.12version>
        dependency>
        <dependency>
            <groupId>org.mybatisgroupId>
            <artifactId>mybatisartifactId>
            <version>3.5.2version>
        dependency>
        <dependency>
            <groupId>org.springframeworkgroupId>
            <artifactId>spring-webmvcartifactId>
            <version>5.3.19version>
        dependency>
        <dependency>
            <groupId>mysqlgroupId>
            <artifactId>mysql-connector-javaartifactId>
            <version>8.0.26version>
        dependency>
        
        <dependency>
            <groupId>org.springframeworkgroupId>
            <artifactId>spring-jdbcartifactId>
            <version>5.3.18version>
        dependency>
        <dependency>
            <groupId>org.aspectjgroupId>
            <artifactId>aspectjweaverartifactId>
            <version>1.9.6version>
        dependency>
        
        <dependency>
            <groupId>org.mybatisgroupId>
            <artifactId>mybatis-springartifactId>
            <version>2.0.6version>
        dependency>
    dependencies>
    
    <build>
        <resources>
            <resource>
                <directory>src/main/javadirectory>
                <includes>
                    <include>**/*.propertiesinclude>
                    <include>**/*.xmlinclude>
                includes>
                <filtering>truefiltering>
            resource>
        resources>
    build>
1.1上面的build配置可以解决静态资源无法导入问题

2.实体类 User

根据数据库写实体类

package com.peng.pojo;
public class User {
    private int id;
    private String name;
    private String pwd;

    public User() {
    }

    public User(int id, String name, String pwd) {
        this.id = id;
        this.name = name;
        this.pwd = pwd;
    }

    public int getId() {
        return id;
    }

    public void setId(int id) {
        this.id = id;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public String getPwd() {
        return pwd;
    }

    public void setPwd(String pwd) {
        this.pwd = pwd;
    }

    @Override
    public String toString() {
        return "User{" +
                "id=" + id +
                ", name='" + name + '\'' +
                ", pwd='" + pwd + '\'' +
                '}';
    }
}
3.接口 UserMapper
import com.peng.pojo.User;

import java.util.List;

public interface UserMapper {
    public List<User> selectUser();
    public int addUser(User user);
    public int deleteUSer(int id);
    public int updateUser(User user);
}
4.SQL实现文件 UserMapper.xml

DOCTYPE mapper
        PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
        "http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<mapper namespace="com.peng.mapper.UserMapper">
    <select id="selectUser" resultType="user">
        select *from mybatis.user
    select>
    <insert id="addUser" parameterType="user" >
        insert into mybatis.user(id, name, pwd) value (#{id},#{name},#{pwd})
    insert>
    <delete id="deleteUSer" parameterType="int" >
        delete from mybatis.user where id=#{id}
    delete>
    <update id="updateUser" parameterType="user" >
        update mybatis.user set name=#{name},pwd=#{pwd} where id=#{id}
    update>
mapper>
5.mybatis-config.xml mybatis基础文件,只留下基础配置例如别名…
 
DOCTYPE configuration
        PUBLIC "-//mybatis.org//DTD Config 3.0//EN"
        "http://mybatis.org/dtd/mybatis-3-config.dtd">

<configuration>
    <typeAliases>
        <package name="com.peng.pojo"/>
    typeAliases>

configuration>
6.spring-mybatis.xml 通过Spring方式配置sqlSessionFactory

<beans xmlns="http://www.springframework.org/schema/beans"
       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
       xmlns:aop="http://www.springframework.org/schema/aop"
       xsi:schemaLocation="http://www.springframework.org/schema/beans
        http://www.springframework.org/schema/beans/spring-beans.xsd
        http://www.springframework.org/schema/aop
        http://www.springframework.org/schema/aop/spring-aop.xsd">
    
    <bean id="dataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource">
        <property name="driverClassName" value="com.mysql.cj.jdbc.Driver"/>
        <property name="url" value="jdbc:mysql://localhost:3306?mybatis&serverTimezone=UTC&characterEncoding=utf8&useUnicode=true"/>
        <property name="username" value="root"/>
        <property name="password" value="1234"/>
    bean>
    <bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
        <property name="dataSource" ref="dataSource" />
        
        <property name="configLocation" value="classpath:mybatis-config.xml"/>
        
        <property name="mapperLocations" value="classpath:com/peng/mapper/*.xml"/>
    bean>
    
    <bean id="sqlSession" class="org.mybatis.spring.SqlSessionTemplate">
        
        <constructor-arg index="0" ref="sqlSessionFactory"/>
    bean>
beans>
7.注入实现类 UserMapperImpl
package com.peng.mapper;

import com.peng.pojo.User;
import org.mybatis.spring.SqlSessionTemplate;

import java.util.List;

public class UserMapperImpl implements UserMapper{
    private SqlSessionTemplate sqlSession;

    public void setSqlSession(SqlSessionTemplate sqlSession) {
        this.sqlSession = sqlSession;
    }


    @Override
    public List<User> selectUser() {
        UserMapper mapper = sqlSession.getMapper(UserMapper.class);
         return  mapper.selectUser();
    }

   @Override
    public int addUser(User user) {
       UserMapper mapper = sqlSession.getMapper(UserMapper.class);
        return mapper.addUser(user);
    }

    @Override
    public int deleteUSer(int id) {
        UserMapper mapper = sqlSession.getMapper(UserMapper.class);
      return mapper.deleteUSer(id);
    }

    @Override
    public int updateUser(User user) {
        UserMapper mapper = sqlSession.getMapper(UserMapper.class);
      return   mapper.updateUser(user);
    }
}

8.applicationContext.xml 导入spring的sqlSessionFactory配置只负责注入实现类

<beans xmlns="http://www.springframework.org/schema/beans"
       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
       xmlns:aop="http://www.springframework.org/schema/aop"
       xsi:schemaLocation="http://www.springframework.org/schema/beans
        http://www.springframework.org/schema/beans/spring-beans.xsd
        http://www.springframework.org/schema/aop
        http://www.springframework.org/schema/aop/spring-aop.xsd">
		<import resource="spring-mybatis.xml"/>
	    <bean id="userMapper" class="com.peng.mapper.UserMapperImpl">
	        <property name="sqlSession" ref="sqlSession"/>
	    bean>
beans>
9.测试类
import com.peng.mapper.UserMapper;
import com.peng.pojo.User;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

import java.util.List;

public class MyTest {
    public static void main(String[] args) {
        ApplicationContext context = new ClassPathXmlApplicationContext("applicationContext.xml");
        UserMapper userMapper = context.getBean("userMapper", UserMapper.class);

       int i = userMapper.addUser(new User(2, "pengge", "123456"));
        if (i>0){
            System.out.println("增加成功");
        }
        int i1 = userMapper.updateUser(new User(1, "龙仔", "111111"));
        if (i1>0){
            System.out.println("修改成功");
        }
        int i2 = userMapper.deleteUSer(7);
        if (i2>0){
            System.out.println("删除成功");
        }
        List<User> users = userMapper.selectUser();
        for (User user : users) {
            System.out.println(user);
        }

    }
}

结果:

欢迎分享,转载请注明来源:内存溢出

原文地址: http://outofmemory.cn/langs/923490.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-05-16
下一篇 2022-05-16

发表评论

登录后才能评论

评论列表(0条)

保存