Home >Java >javaTutorial >Introduction to 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 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 is a J2EE application framework, a lightweight IoC and AOP container framework, mainly a lightweight container that manages the life cycle of javaBeans.
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
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("添加成功"); } }
<?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
<?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>
Run the model--Main method, you can see that a record is added to the database
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!