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

 

结果

添加

更改

 

 

删除

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


 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值