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和mybais1.在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配置介绍的详细内容。