Spring+Mybatis增删改查案例

  1. 首先创建bean类

public class User {
  public int id;
  public String name;
  public User() {
    // TODO Auto-generated constructor stub
}

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

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;
}
@Override
public String toString() {
    return "User [id=" + id + ", name=" + name + "]";
}

}

2.创建对应的Mapper映射xml

"-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd">
"com.lei.bean.User">
    "com.lei.bean.User" id="user">
        <id column="id" property="id"/>
        <result column="name" property="name" />
    

    

      

      

     id="insert" parameterType="com.lei.bean.User">
          insert into user values(#{id},#{name})
     

     id="deleteId" parameterType="int">
          delete from user where id=#{id}
     


     id="deleteName" parameterType="String">
          delete from user where name=#{name}
     

     id="update" parameterType="com.lei.bean.User">
          update user set name=#{name} where id=#{id}
     

3.创建操作接口


public interface UserDao {

  public User getUser(int id);
  public User getUser(String name);
  public User getUser();
  public void addUser(User user);
  public void deleteUser(int id);
  public void deleteUser(String name);
  public void updateUser(User user);
}

4.实现接口


public class UserDaoImpl implements UserDao {
    @Autowired
    private SqlSessionTemplate sqlSessionTemplate;
    public User getUser(int id) {
        // TODO Auto-generated method stub
        return sqlSessionTemplate.selectOne("getOneUserId",id);
    }

    public User getUser(String name) {
        // TODO Auto-generated method stub
        return sqlSessionTemplate.selectOne("getOneUserName",name);
    }

    public User getUser() {
        // TODO Auto-generated method stub
        return sqlSessionTemplate.selectOne("getAllUser");
    }

    public void addUser(User user) {
        // TODO Auto-generated method stub
        sqlSessionTemplate.insert("insert", user);
    }

    public void deleteUser(int id) {
        // TODO Auto-generated method stub
        sqlSessionTemplate.delete("deleteId", id);
    }

    public void deleteUser(String name) {
        // TODO Auto-generated method stub
        sqlSessionTemplate.delete("deleteName",name);
    }

    public void updateUser(User user) {
        // TODO Auto-generated method stub
        sqlSessionTemplate.update("update", user);
    }

}

5.测试类


public class Test {

    public static void main(String[] args) {
        // TODO Auto-generated method stub

        String[] locations = {"beans.xml"};     
        ApplicationContext ctx =      
            new ClassPathXmlApplicationContext(locations);  
        UserDao userDao=(UserDao) ctx.getBean("userdaoimpl");
       // User user=userDao.getUser("yu");        
        //System.out.println(user);
       // userDao.addUser(new User(4, "yang"));
        //userDao.deleteUser(4);
        //userDao.deleteUser("lei");
        userDao.updateUser(new User(2, "wang"));


    }

}

6.beans.xml文件配置

     
<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-2.5.xsd"> 


      
     <bean class="org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor"/>  
      
     <bean id="propertyconfigur" class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
       <property name="locations">
          <list>
            <value>classpath:jdbc.propertiesvalue>
          list>
       property>
     bean>
      
     <bean id="DataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource"
        destroy-method="close">
        <property name="driverClass" value="${jdbc.driverClassName}">property>
        <property name="jdbcUrl" value="${jdbc.url}">property>
        <property name="user" value="${jdbc.username}">property>
        <property name="password" value="${jdbc.password}">property>
     bean>
    
    <bean id="SqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
        <property name="dataSource" ref="DataSource" />
        <property name="mapperLocations">
            <value>classpath*:com/lei/bean/mapper/*Mapper.xmlvalue>
        property>
    bean>

    
    <bean id="SqlSessionTemplate" class="org.mybatis.spring.SqlSessionTemplate">  
        <constructor-arg index="0" ref="SqlSessionFactory" />
    bean>
    
    <bean id="userdaoimpl" class="com.lei.dao.impl.UserDaoImpl">
    bean>
beans>  

7.项目下载地址
http://download.csdn.net/detail/qq_22222499/9822898

你可能感兴趣的:(java-web,mysql,spring,mybatis,增删改查)