整合Spring、Mybatisd对单表进行增删查改

这段时间正在学习Spring和Mybatis,自己写的一个小实验,欢迎大家交流学习

1.创建一个maven项目

整合Spring、Mybatisd对单表进行增删查改_第1张图片

2.整理好项目目录结构

整合Spring、Mybatisd对单表进行增删查改_第2张图片

3.在pom.xml文件里面导入依赖

</properties>

  <dependencies>
    <dependency>
      <groupId>junit</groupId>
      <artifactId>junit</artifactId>
      <version>4.11</version>
      <scope>test</scope>
    </dependency>


    <!-- https://mvnrepository.com/artifact/org.springframework/spring-core -->
    <dependency>
      <groupId>org.springframework</groupId>
      <artifactId>spring-core</artifactId>
      <version>5.0.2.RELEASE</version>
    </dependency>

    <!-- https://mvnrepository.com/artifact/org.springframework/spring-beans -->
    <dependency>
      <groupId>org.springframework</groupId>
      <artifactId>spring-beans</artifactId>
      <version>5.0.2.RELEASE</version>
    </dependency>

    <!-- https://mvnrepository.com/artifact/org.springframework/spring-context -->
    <dependency>
      <groupId>org.springframework</groupId>
      <artifactId>spring-context</artifactId>
      <version>5.0.2.RELEASE</version>
    </dependency>

    <!-- https://mvnrepository.com/artifact/commons-beanutils/commons-beanutils -->
    <dependency>
      <groupId>commons-beanutils</groupId>
      <artifactId>commons-beanutils</artifactId>
      <version>1.9.4</version>
    </dependency>


    <!-- https://mvnrepository.com/artifact/dom4j/dom4j -->
    <dependency>
      <groupId>dom4j</groupId>
      <artifactId>dom4j</artifactId>
      <version>1.6.1</version>
    </dependency>


    <!-- https://mvnrepository.com/artifact/org.aspectj/aspectjweaver -->
    <dependency>
      <groupId>org.aspectj</groupId>
      <artifactId>aspectjweaver</artifactId>
      <version>1.8.13</version>
    </dependency>


    <!-- https://mvnrepository.com/artifact/org.springframework/spring-jdbc -->
    <dependency>
      <groupId>org.springframework</groupId>
      <artifactId>spring-jdbc</artifactId>
      <version>5.0.2.RELEASE</version>
    </dependency>


    <!-- https://mvnrepository.com/artifact/org.springframework/spring-aop -->
    <dependency>
      <groupId>org.springframework</groupId>
      <artifactId>spring-aop</artifactId>
      <version>5.0.2.RELEASE</version>
    </dependency>

    <!-- https://mvnrepository.com/artifact/org.springframework/spring-aspects -->
    <dependency>
      <groupId>org.springframework</groupId>
      <artifactId>spring-aspects</artifactId>
      <version>5.0.2.RELEASE</version>
    </dependency>

    <dependency>
      <groupId>org.mybatis</groupId>
      <artifactId>mybatis-spring</artifactId>
      <version>2.0.4</version>
    </dependency>

    <dependency>
      <groupId>org.mybatis</groupId>
      <artifactId>mybatis</artifactId>
      <version>3.5.1</version>
    </dependency>

    <!-- https://mvnrepository.com/artifact/mysql/mysql-connector-java -->
    <dependency>
      <groupId>mysql</groupId>
      <artifactId>mysql-connector-java</artifactId>
      <version>5.1.38</version>
    </dependency>

  </dependencies>

4.配置好applicatioinContext.xml文件和UserMapper.xml

这里需要注意UserMapper.xml在resource的位置需要和UserMapper在java的位置相同

applicatioinContext.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"
       xmlns:mybatis="http://mybatis.org/schema/mybatis-spring" xmlns:tx="http://www.springframework.org/schema/tx"
       xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd http://mybatis.org/schema/mybatis-spring http://mybatis.org/schema/mybatis-spring.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx.xsd">

    <context:component-scan base-package="com.lp.mybatisspring"/>

    <!--
      配置数据源数据库连接 -->

    <bean id="dataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource">
        <property name="username" value="root"/>
        <property name="password" value="123456"/>
        <property name="url" value="jdbc:mysql://localhost:3306/mybatis?characterEncoding=utf8&useSSL=false"/>
        <property name="driverClassName" value="com.mysql.jdbc.Driver"/>
    </bean>

    <!--  事务管理器 完成手动事务管理-->
    <bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
        <property name="dataSource" ref="dataSource"/>
    </bean>

    <bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
        <property name="dataSource" ref="dataSource" />
    </bean>

    <mybatis:scan base-package="com.lp.mybatisspring.dao"/>

    <tx:annotation-driven transaction-manager="transactionManager"/>

</beans>

将数据库操作写在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">
<!--命名空间-->
<!--书写user表的sql语句
比如:增删查改-->
<!--每一个sql语句都有一个唯一id-->

<mapper namespace="com.lp.mybatisspring.dao.UserMapper">
    <select id="getUserById" parameterType="int" resultType="com.lp.mybatisspring.bean.User">
    select id,username from users where id = #{
     id}
  </select>
    <!--   keyProperty  获取到的新插入的记录的主键赋值给返回对象的属性 -->
    <insert id="saveUser" parameterType="com.lp.mybatisspring.bean.User" useGeneratedKeys="true" keyProperty="id">
        insert into users(username) values(#{
     username});
    </insert>

    <!-- 多个用户对象 , mybatis  会自动判断多个User,自动封装成集合   -->
    <select id="listUser" resultType="com.lp.mybatisspring.bean.User">
        select id,username from users ;
    </select>


    <update id="updateUser" parameterType="com.lp.mybatisspring.bean.User" >
        update users set username=#{
     username} where id=#{
     id}
    </update>

    <delete id="deleteUser" parameterType="int">
          delete from users where id=#{
     id}
    </delete>
</mapper>

5.在bean中写好user实体类

切记与你自己数据库里的属性对应
例如:
整合Spring、Mybatisd对单表进行增删查改_第3张图片与其对应的user类

package com.lp.mybatisspring.bean;
public class User {
     
    private  Integer id;
    private String username;

    public Integer getId() {
     
        return id;
    }

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

    public String getUsername() {
     
        return username;
    }

    public void setUsername(String username) {
     
        this.username = username;
    }

    @Override
    public String toString() {
     
        return "User{" +
                "id=" + id +
                ", username='" + username + '\'' +
                '}';
    }

}

6.写好三层架构

整合Spring、Mybatisd对单表进行增删查改_第4张图片

Dao层里面的UserMapper接口写好要实现的方法:

package com.lp.mybatisspring.dao;
import com.lp.mybatisspring.bean.User;
import org.springframework.stereotype.Repository;

import java.util.List;


@Repository
public interface UserMapper {
     
    public  User getUserById(int id) ;

    public boolean saveUser(User user);

    public List<User> listUser();

    void updateUser(User user);

    void deleteUser(int id);

 /*   *//*查找用户*//*
    User selectUser(int id);
    *//*根据id修改用户信息*//*
    int updateById(User user);
    *//*插入数据*//*
    int insertPerson(User user);
    *//*删除用户信息*//*
    int deleteAll(User user);
    */

}

service层:UserService接口

package com.lp.mybatisspring.service;

import com.lp.mybatisspring.bean.User;
import org.springframework.stereotype.Repository;

import java.util.List;


public interface UserService {
     
    public User getUserById(int id);


    public boolean save(User user);

    public List<User> listUser();

    void updateUser(User user);

    void deleteUser(int id);
}

UserServiceimlp类:

package com.lp.mybatisspring.service.impl;

import com.lp.mybatisspring.bean.User;

import com.lp.mybatisspring.dao.UserMapper;
import com.lp.mybatisspring.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

@Service()
public class UserServiceimpl implements UserService{
     

    @Autowired
    private UserMapper userMapper;

    @Override
    @Transactional(readOnly = true)
    public User getUserById(int id) {
     
        return userMapper.getUserById(id);
    }

    @Override
    @Transactional
    public boolean save(User user) {
     
        boolean flag = userMapper.saveUser(user);
        System.out.println(user);
        return flag;
    }

    @Override
    public List<User> listUser() {
     
        return userMapper.listUser();
    }

    @Override
    public void updateUser(User user) {
     
        userMapper.updateUser(user);
    }

    @Override
    public void deleteUser(int id) {
     
        userMapper.deleteUser(id);
    }

}

Controller层实现方法

package com.lp.mybatisspring.controller;

import com.lp.mybatisspring.bean.User;
import com.lp.mybatisspring.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;

@Controller
public class UserController {
     
    @Autowired
    private UserService userService;

    public void test(){
     
       // User user = userService.getUserById(4);
      //  user.setUsername("jack");
           //   System.out.println(user);



       /* User user = new User(null,"lp");
        boolean flag = userService.save(user);
        System.out.println(flag);*/

       /* List userList = userService.listUser();
        for (User user : userList) {
            System.out.println(user);
        }*/

        //userService.updateUser(user);
        userService.deleteUser(4);


    }
}

7.demo里面Test类测试

package com.lp.mybatisspring.demo;

import com.lp.mybatisspring.controller.UserController;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;



public class Test {
     
    public static void main(String[] args) {
     
        ApplicationContext context =new ClassPathXmlApplicationContext("com/lp/mybatisspring/config/applicatioinContext.xml");
        UserController userController= (UserController) context.getBean("userController");
        userController.test();
    }
}

需要源码的欢迎留言,我经常在线

菜鸡一枚,欢迎大家交流学习

你可能感兴趣的:(java,spring,后端)