spring+mybatis+mysql
mybatis jar包 https://code.google.com/p/mybatis/downloads/list?can=3&q=Product%3DMyBatis
mybatis整合springjar包 https://code.google.com/p/mybatis/downloads/list?can=3&q=Product%3DSpring
首先创建数据库
CREATE TABLE emp ( id int) auto_increment, name varchar(20) NOT NULL, age int NOT NULL, PRIMARY KEY (id) ) ENGINE=INNODB;
新建一个web项目
创建一个数据库实体类
package entity; import java.io.Serializable; public class Emp implements Serializable { private static final long serialVersionUID = 1311811898794089205L; private Integer id; private String name; private Integer age; 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 Integer getAge() { return age; } public void setAge(Integer age) { this.age = age; } }
创建EmpDao(接口)
package dao; import entity.Emp; public interface EmpDao { public Emp getById(Integer id); public void update(Emp emp); }
创建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> <typeAliases > <typeAlias type="entity.Emp" alias="emp"/><!-- 给实体类起别名 --> </typeAliases> <mappers> <mapper resource="mapper/EmpDaoMapper.xml"/> <!-- 导入子配置文件 --> </mappers> </configuration>
创建EmpDaoMapper.xml(EmpDao配置文件)
<?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.EmpDao"><!-- 指向dao接口 --> <select id="getById" parameterType="Integer" resultType="entity.Emp"> <![CDATA[ select * from emp where id=#{id} ]]> </select> <update id="update" parameterType="emp"><!-- emp 是 Emp实体类的别名 在总配置文件(在这里指的是mybatis-config.xml)中定义 --> <![CDATA[ update emp set name=#{name},age=#{age} where id=#{id} ]]> </update> </mapper>
创建EmpService(接口)
package service; import entity.Emp; public interface EmpService { public Emp getById(Integer id); public void update(Emp emp); }
创建EmpServiceImpl(EmpService实现类)
package service.impl; import dao.EmpDao; import entity.Emp; import service.EmpService; public class EmpServiceImpl implements EmpService { private EmpDao empDao; @Override public Emp getById(Integer id) { return empDao.getById(id); } public void setEmpDao(EmpDao empDao) { this.empDao = empDao; } @Override public void update(Emp emp) { empDao.update(emp); } }
创建spring配置文件
<?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:p="http://www.springframework.org/schema/p" xmlns:context="http://www.springframework.org/schema/context" 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 "> <!-- 需要导入context命名空间 是用来读取jdbc属性文件 --> <context:property-placeholder location="classpath:mysql.properties"/> <!-- 第一种数据源 不用导jar包 --> <bean id="dataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource"> <property name="driverClassName" value="${driverClassName}"/> <property name="url" value="${url}"/> <property name="username" value="${username}"/> <property name="password" value="${password}"/> </bean> <!-- 第二种方式配置数据源(dbcp方式) 需要导commons-pool.jar 和 commons-dbcp.jar <bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource"> <property name="driverClassName" value="${driverClassName}"/> <property name="url" value="${url}"/> <property name="username" value="${username}"/> <property name="password" value="${password}"/> </bean> --> <bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean"> <property name="configLocation" value="classpath:mybatis-config.xml"/> <property name="dataSource" ref="dataSource"/> </bean> <bean id="empDao" class="org.mybatis.spring.mapper.MapperFactoryBean"> <property name="mapperInterface" value="dao.EmpDao"/> <property name="sqlSessionFactory" ref="sqlSessionFactory"/> </bean> <bean id="empService" class="service.impl.EmpServiceImpl"> <property name="empDao" ref="empDao"/> </bean> </beans>
如果配置数据源时,想引用属性文件中的字段 可以新建一个属性文件
driverClassName=com.mysql.jdbc.Driver url=jdbc:mysql://localhost:3306/xx username=root password=
测试
package test; import org.springframework.context.ApplicationContext; import org.springframework.context.support.ClassPathXmlApplicationContext; import entity.Emp; import service.EmpService; public class TestMyBatis { public static void main(String[] args) { ApplicationContext ac=new ClassPathXmlApplicationContext("applicationContext.xml"); EmpService empService= (EmpService) ac.getBean("empService"); Emp emp=empService.getById(1); System.out.println(emp.getName()); emp.setName("少年2"); empService.update(emp); emp=empService.getById(1); System.out.println("新名字"+emp.getName()); } }
结果
少年 新名字少年2