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

MyBatis笔记----Mybatis3.4.2与spring4整合:增删查改_第1张图片

 

 

 

 

 

 

 

 

结构图

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

MyBatis笔记----Mybatis3.4.2与spring4整合:增删查改_第2张图片

包图

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

MyBatis笔记----Mybatis3.4.2与spring4整合:增删查改_第3张图片

数据库图

MyBatis笔记----Mybatis3.4.2与spring4整合:增删查改_第4张图片

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>

  <mappers>
  <mapper resource="com/ij34/mybatis/UserMapper.xml"/>

  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">
    
  <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");
}
}

 

结果

MyBatis笔记----Mybatis3.4.2与spring4整合:增删查改_第5张图片

添加

MyBatis笔记----Mybatis3.4.2与spring4整合:增删查改_第6张图片

更改

MyBatis笔记----Mybatis3.4.2与spring4整合:增删查改_第7张图片

MyBatis笔记----Mybatis3.4.2与spring4整合:增删查改_第8张图片

MyBatis笔记----Mybatis3.4.2与spring4整合:增删查改_第9张图片

 

 

删除

MyBatis笔记----Mybatis3.4.2与spring4整合:增删查改_第10张图片

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


 

你可能感兴趣的:(java,数据库)