今天这个是mybatis与spring的整合
依旧是一个查询的demo
首先是demo的结构
然后是我的jdbc.properties
jdbc.driverClassName=com.mysql.jdbc.Driver
jdbc.url=jdbc:mysql:///test?createDatabaseIfNotExist=true&useUnicode=true&characterEncoding=utf8&autoReconnect=true&failOverReadOnly=false
jdbc.username=root
jdbc.password=root
与我的实体
package com.fei.model;
/**
* Created by fei on 2015/9/8.
*/
public class User {
private Integer id;
private String name;
private String password;
private Integer age;
public User(String name, String password, int age) {
this.name=name;
this.password=password;
this.age=age;
}
public User() {
}
public Integer getId() {
return id;
}
public void setId(Integer id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getPassword() {
return password;
}
public void setPassword(String password) {
this.password = password;
}
public Integer getAge() {
return age;
}
public void setAge(Integer age) {
this.age = age;
}
@Override
public String toString(){
return "User[id="+id+" , name="+name+" , password="+password+"]";
}
}
这个数据库就自己弄了哈
下面是两个配置文件,分别是mybatis的与spring的,网上的整合方式有三种,我这个就做了一种
mybatis-conf.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="mapper/userMapper.xml"/>
</mappers>
</configuration>
spring是这个样子的
<beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:p="http://www.springframework.org/schema/p" xmlns:context="http://www.springframework.org/schema/context"
xmlns:jee="http://www.springframework.org/schema/jee" xmlns:tx="http://www.springframework.org/schema/tx"
xmlns:aop="http://www.springframework.org/schema/aop"
xsi:schemaLocation="
http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.0.xsd
http://www.springframework.org/schema/jee http://www.springframework.org/schema/jee/spring-jee-3.0.xsd
http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.0.xsd
http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-3.0.xsd">
<context:property-placeholder location="classpath:jdbc.properties"/>
<bean id="jdbcDataSource" class="org.apache.commons.dbcp.BasicDataSource" destroy-method="close"
p:driverClassName="${jdbc.driverClassName}"
p:url="${jdbc.url}" p:username="${jdbc.username}" p:password="${jdbc.password}"></bean>
<!--
<bean id="jdbcDataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource">
<property name="driverClassName">
<value>com.fei</value>
</property>
<property name="url">
<value>jdbc:mysql://localhost:3306/test?useUnicode=true&characterEncoding=UTF-8</value>
</property>
<property name="username">
<value>root</value>
</property>
<property name="password">
<value>root</value>
</property>
</bean>
-->
<bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
<property name="dataSource" ref="jdbcDataSource"/>
<property name="configLocation" value="classpath:mybatis-config.xml"></property>
</bean>
<bean id="userDao" class="org.mybatis.spring.mapper.MapperFactoryBean">
<property name="mapperInterface" value="com.fei.dao.UserDao"></property>
<property name="sqlSessionFactory" ref="sqlSessionFactory"></property>
</bean>
</beans>
不要说看不懂这个,这里只剩下需要的了
下面就是mybatis的mapper
usermapper.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="com.fei.dao.UserDao">
<select id="getUser" parameterType="com.fei.model.User" resultType="com.fei.model.User">
SELECT * FROM user WHERE name=#{name} AND password=#{password}
</select>
<select id="getUserById" parameterType="com.fei.model.User" resultType="com.fei.model.User">
SELECT * FROM user WHERE id=#{id}
</select>
<insert id="addUser" parameterType="com.fei.model.User" flushCache="true">
INSERT INTO user (name,password,age) VALUES (#{name},${password},#{age})
</insert>
</mapper>
接下来是dao了
package com.fei.dao;
import com.fei.model.User;
/**
* Created by fei on 2015/9/8.
*/
public interface UserDao {
public User getUser(User user);
public User getUserById(Integer id);
public void addUser(User user);
/*
public void updateUser(User user);
public void deleteUser(int UserId);*/
}
这里就把程序搭建好了(没有web部分,这个就是mybatis与spring的整合而已,springmvc再说)
执行main
package com.fei.test;
import com.fei.dao.UserDao;
import com.fei.model.User;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
/**
* Created by fei on 2015/9/8.
*/
public class Main {
public static void main(String[] args) {
ApplicationContext ctx=null;
ctx=new ClassPathXmlApplicationContext("application.xml");
UserDao userDao=(UserDao) ctx.getBean("userDao");
/* User user=new User("test","1234",12);
userDao.addUser(user);*/
User user=new User();
user.setName("test");
user.setPassword("1234");
User newUser=userDao.getUser(user);
System.out.println(newUser.toString());
}
}
对,就是这样,简单吧
此处有demo 15kb哦,绝对可用