• MyBatis笔记----Mybatis3.4.2与spring4整合:增删查改


    结构图

    刚之前没什么区别,多了一个applicationContext.xml

    包图

    由于之前出了一点错误,有些包可能多加上了

    数据库图

    model

    User.java

    package com.ij34.model;
    
    public class User {
      private int id;
      private String name;
      private int age;
    
    
      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 int getAge() {
        return age;
    }
    
    public void setAge(int age) {
        this.age = age;
    }
    public String toString() {
        return "User [id=" + id + ", name=" + name + ", age=" + age + "]";
    }
    
    
    }

    UserMapper.java

    package com.ij34.model;
    
    
    public interface UserMapper {
         
        public User selectUser(int id);
        public void insertUser(User user);
        public void updateUser(User user);
        public void deleteUser(String name);
    }

    xml

    UserMapper.xml

    <?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.ij34.model.UserMapper">
      <select id="selectUser" parameterType="int" resultType="User">
        select * from users where id=#{id};
      </select>
        <update id="updateUser" keyProperty="id">
        update users set name=#{name},age=#{age} where id=#{id}
      </update>
      
      <insert id="insertUser" >
       insert into users(name,age)values(#{name},#{age})
      </insert>
      <delete id="deleteUser" >
        delete from users where name=#{name}
      </delete>
    
      </mapper>

    mybatis-config.xml

    <?xml version="1.0" encoding="UTF-8" ?>
    <!DOCTYPE configuration
      PUBLIC "-//mybatis.org//DTD Config 3.0//EN"
      "http://mybatis.org/dtd/mybatis-3-config.dtd">
    <configuration>
    <typeAliases>
    <typeAlias type="com.ij34.model.User" alias="User"/>
    </typeAliases>
    <!--  <environments default="development">
        <environment id="development">
          <transactionManager type="JDBC"/>
          <dataSource type="POOLED">
            <property name="driver" value="com.mysql.jdbc.Driver"/>
            <property name="url" value="jdbc:mysql://localhost:3306/mybatis?useSSL=true"/>
            <property name="username" value="root"/>
            <property name="password" value="123456"/>
          </dataSource>
        </environment>
      </environments>  -->
      <mappers>
      <mapper resource="com/ij34/mybatis/UserMapper.xml"/>
    <!--    <mapper class="com.ij34.model.UserMapper"/> -->
      </mappers>
    </configuration>

    applicationContext.xml

    <?xml version="1.0" encoding="UTF-8"?>
    <beans xmlns="http://www.springframework.org/schema/beans"
           xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
           xmlns:context="http://www.springframework.org/schema/context"
           xsi:schemaLocation="
                http://www.springframework.org/schema/beans
                http://www.springframework.org/schema/beans/spring-beans-4.3.xsd
                http://www.springframework.org/schema/context
                http://www.springframework.org/schema/context/spring-context-4.3.xsd">
        <!-- Showcase's CustomFreemarkerManager example -->
      <bean id="dataSource" class="org.apache.commons.dbcp2.BasicDataSource">
         <property name="driverClassName" value="com.mysql.jdbc.Driver"></property> 
         <property name="url" value="jdbc:mysql://localhost:3306/mybatis"></property> 
         <property name="username" value="root"></property> 
         <property name="password" value="123456"></property> 
      </bean> 
      <bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
          <property name="dataSource" ref="dataSource"></property>
          <property name="configLocation" value="com/ij34/mybatis/mybatis-config.xml"></property>
      </bean>
      <bean id="UserMapperFactory" class="org.mybatis.spring.mapper.MapperFactoryBean">
          <property name="sqlSessionFactory" ref="sqlSessionFactory"></property>
          <property name="mapperInterface" value="com.ij34.model.UserMapper"></property>
      </bean>
    </beans>

    测试

    package com.ij34.bean;
    
    import java.io.IOException;
    //import java.io.InputStream;
    //import java.util.List;
    //
    //import org.apache.ibatis.io.Resources;
    //import org.apache.ibatis.session.SqlSession;
    //import org.apache.ibatis.session.SqlSessionFactory;
    //import org.apache.ibatis.session.SqlSessionFactoryBuilder;
    import org.springframework.context.ApplicationContext;
    import org.springframework.context.support.ClassPathXmlApplicationContext;
    
    import com.ij34.model.*;
    public class Test {
    public static void main(String[] args) throws IOException {
       @SuppressWarnings("resource")
    ApplicationContext ac=new ClassPathXmlApplicationContext("com/ij34/mybatis/applicationContext.xml");
       UserMapper mapper=(UserMapper) ac.getBean("UserMapperFactory");
       User user=mapper.selectUser(3);  //查找
       System.out.println(user);
    /*     User user=new User();
         user.setId(9);
         user.setName("小测01");
         user.setAge(22);
         mapper.insertUser(user);  //添加
         System.out.println(user);*/
    /*    User user=mapper.selectUser(8);
        user.setName("大大A");
        user.setAge(28);
        mapper.updateUser(user);  //更改
        System.out.println(user);*/
     //  mapper.deleteUser("小测01");  //删除
     //  System.out.println("已经删除:小测01");
    }
    }

    结果

    添加

    更改

    删除

    ---------多动手,少打字


  • 相关阅读:
    logging模块
    获得本机时间
    为了节约一台打印机,我也是无奈呀~~~~
    django通过管理页上传图片
    python打印爱心
    djago后台管理页面
    今天休年休找不到填年休的表了,结果就写了一个查找文件的缩引存起来方便下次用
    中间件调用顺序_______网站仿问过程
    django中间件
    py3.8安装
  • 原文地址:https://www.cnblogs.com/tk55/p/6670155.html
Copyright © 2020-2023  润新知