bean对象定义:
package com.mybatis.bean; public class Person { 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; } }
mappper接口,通过注解方式定义SQL语句
package com.mybatis.inter; import java.util.List; import org.apache.ibatis.annotations.Delete; import org.apache.ibatis.annotations.Insert; import org.apache.ibatis.annotations.Select; import org.apache.ibatis.annotations.Update; import com.mybatis.bean.Person; public interface PersonDAO { @Insert("insert into person(name,age) values(#{name},#{age})") public void add(Person person); @Delete("delete from person where id=#{id}") public void delete(int id); @Select("select * from person where id=#{id}") public Person queryById(int id); @Select("select * from person") public List<Person> findAll(); @Update("update person set name=#{name},age=#{age} where id=#{id}") public void update(Person person); }
mybatis-config.xml配置,数据源配置转到Spring文件中,此处只声明mapper,class属性指向mapper接口
<?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 class="com.mybatis.inter.PersonDAO"/> </mappers> </configuration>
数据库属性文件定义persistence.properties
driverClassName=com.mysql.jdbc.Driver url=jdbc:mysql://127.0.0.1:3306/test username=root password=root
Spring配置文件
<?xml version="1.0" encoding="UTF-8"?> <beans xmlns="http://www.springframework.org/schema/beans" xmlns:context="http://www.springframework.org/schema/context" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:tx="http://www.springframework.org/schema/tx" xmlns:aop="http://www.springframework.org/schema/aop" xmlns:util="http://www.springframework.org/schema/util" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx.xsd http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop.xsd http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util.xsd"> <bean class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer"> <property name="locations"> <list> <value>classpath:persistence.properties</value> </list> </property> </bean> <bean id="jdbcDataSource" 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> <bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean"> <property name="dataSource" ref="jdbcDataSource" /> <property name="configLocation" value="classpath:mybatis-config.xml" /> </bean> <bean id="personDAO" class="org.mybatis.spring.mapper.MapperFactoryBean"> <property name="mapperInterface" value="com.mybatis.inter.PersonDAO" /> <property name="sqlSessionFactory" ref="sqlSessionFactory" /> </bean> </beans>
测试:
package com.mybatis.test; import java.util.List; import org.springframework.context.ApplicationContext; import org.springframework.context.support.ClassPathXmlApplicationContext; import com.mybatis.bean.Person; import com.mybatis.inter.PersonDAO; public class Test1 { @SuppressWarnings("resource") public static void main(String[] args) { ApplicationContext context=null; context=new ClassPathXmlApplicationContext("applicationContext.xml"); PersonDAO personDAO = (PersonDAO) context.getBean("personDAO"); System.out.println("====================test=========================="); // Person person=new Person(); // person.setName("aaa"); // person.setAge(12); // personDAO.add(person); List<Person> list = personDAO.findAll(); for(Person person:list) { System.err.println(person.getName()+" "+person.getAge()); } } }
工程截图:
时间: 2024-09-30 02:10:00