Home >Java >javaTutorial >Introduction to Spring+Mybatis configuration

Introduction to Spring+Mybatis configuration

巴扎黑
巴扎黑Original
2017-07-23 14:50:361599browse

Spring+Mybatis configuration

I used the spring+mybatis framework when doing projects before, and I always wanted to take the time to sort it out

Mybatis:

mybatis supports ordinary SQL queries , an excellent persistence layer framework for stored procedures and advanced mapping. MyBatis eliminates almost all JDBC code and manual setting of parameters and
retrieval of result sets. MyBatis uses simple XML or annotations for configuration and original mapping, mapping interfaces and Java POJOs (Plain Old Java Objects, ordinary Java
objects) into records in the database.

Spring:

spring is a J2EE application framework, a lightweight IoC and AOP container framework, mainly a lightweight container that manages the life cycle of javaBeans.

Start integrating spring and mybais

1. Create a new spring project in IDEA (or create a web project in Eclipse), and you need to import the following jar files.

Introduction to Spring+Mybatis configuration

Paste and copy the required jar package into lib. It cannot be used immediately after copying. You can see that there is no triangle symbol after copying. You need to go to File--ProjectStructure- -Libraries---Add
Introduction to Spring+Mybatis configuration

Introduction to Spring+Mybatis configuration

2. Create a database (spring) and a table (user)

Introduction to Spring+Mybatis configuration

3. Configure in the project

Introduction to Spring+Mybatis configuration

model---User

package 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;
}
}

model--UserDao

package dao;

import model.User;
public interface UserDao {
  public User getUser(User user);
  public void addUser(User user);
  public void updateUser(User user);
  public void deleteUser(int UserId);
}

model--Main

package model;

import dao.UserDao;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;


public class Main {
public static void main(String[] args){
    User user=new User();
    user.setId(1);
    user.setName("jane");
    user.setAge(11);
    ApplicationContext ctx=new ClassPathXmlApplicationContext("ApplicationContext.xml");
    UserDao userDao= (UserDao) ctx.getBean("userDao");
    userDao.addUser(user);
    System.out.println("添加成功");

}
}

4.spring configuration file--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/context ">

<context:property-placeholder location="classpath:spring-jdbc.properties" />
<!-- 配置数据源 -->
<bean id="jdbcDataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource" >
    <property name="username" value="${jdbc.user}"></property>
    <property name="password" value="${jdbc.password}"></property>
    <property name="driverClassName" value="${jdbc.driverClass}"></property>
    <property name="url" value="${jdbc.jdbcUrl}"></property>

</bean>
<bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
    <property name="dataSource" ref="jdbcDataSource"></property>
    <property name="configLocation" value="classpath:mybatis-config.xml"></property>
</bean>
<bean id="userDao" class="org.mybatis.spring.mapper.MapperFactoryBean">
        <property name="mapperInterface" value="dao.UserDao"></property>
        <property name="sqlSessionFactory" ref="sqlSessionFactory"></property>
</bean>
</beans>

spring-jdbc.properties

jdbc.user=root
jdbc.password=12345
jdbc.driverClass=com.mysql.jdbc.Driver
jdbc.jdbcUrl=jdbc:mysql://localhost:3306/spring?useSSL=false

5.mybatis configuration file-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>
<mappers>
    <mapper resource="UserDao.xml"/>
</mappers>
</configuration>

UserDao.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="dao.UserDao">
<select id="getUser" parameterType="model.User" resultType="model.User">
    SELECT * FROM user WHERE  age=#{age}
</select>
<insert id="addUser" parameterType="model.User" flushCache="true">
    INSERT into user (id,name,age)VALUES (#{id},#{name},#{age})
</insert>
<update id="updateUser" parameterType="model.User">
    UPDATE SET user name=#{name} WHERE  id=#{id}
</update>
<delete id="deleteUser" parameterType="int">
       DELETE  FROM user WHERE id=#{id}
</delete>
</mapper>

6. Test it

Run the model--Main method, you can see that a record is added to the database
Introduction to Spring+Mybatis configuration

The above is the detailed content of Introduction to Spring+Mybatis configuration. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn