关于Spring+Mybatis配置介绍
程序员文章站
2022-04-11 18:06:30
...
Spring+Mybatis配置
之前做项目的时候用到了spring+mybatis框架,一直想抽空整理一下
Mybatis:
mybatis是支持普通 SQL查询,存储过程和高级映射的优秀持久层框架。MyBatis 消除了几乎所有的JDBC代码和参数的手工设置以及
结果集的检索。MyBatis 使用简单的 XML或注解用于配置和原始映射,将接口和 Java 的POJOs(Plain Old Java Objects,普通的 Java
对象)映射成数据库中的记录。
Spring:
spring是J2EE应用程序框架,是轻量级的IoC和AOP的容器框架,主要是针对javaBean的生命周期进行管理的轻量级容器。
开始整合spring和mybais
1.在IDEA中新建一个spring项目(或在Eclipse中建立一个web项目),需要导入如下jar文件。
将需要的jar包粘贴复制到lib下,复制完之后不能立马使用,可以看到复制之后并没有三角形符号,需要到File--ProjectStructure--Libraries---添加
2.创建一个数据库(spring)和一个表(user)
3.在项目中进行配置
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配置文件--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配置文件-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.测试一下
运行model--Main方法,可以看到数据库中添加了一条记录
以上就是关于Spring+Mybatis配置介绍的详细内容,更多请关注其它相关文章!