之前做專案的時候用到了spring+mybatis框架,一直想抽空整理一下
mybatis是支援普通SQL查詢,預存程序和進階映射的優秀持久層框架。 MyBatis 幾乎消除了所有的JDBC程式碼和參數的手動設定以及
結果集的檢索。 MyBatis 使用簡單的 XML或註解用於配置和原始映射,將介面和 Java 的POJOs(Plain Old Java Objects,普通的 Java
物件)映射成資料庫中的記錄。
spring是J2EE應用程式框架,是輕量級的IoC和AOP的容器框架,主要是針對javaBean的生命週期進行管理的輕量級容器。
將需要的jar包貼上複製到lib下,複製完之後不能立刻使用,可以看到複製之後並沒有三角形符號,需要到File--ProjectStructure- -Libraries---新增
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>
執行model--Main方法,可以看到資料庫中新增了一筆記錄
以上是關於Spring+Mybatis配置介紹的詳細內容。更多資訊請關注PHP中文網其他相關文章!