标签:
1、工程中引入包:
mybatis-3.2.7、mysql-connector-java-5.1.22-bin
2、添加配置文件:
<?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 alias="Person" type="com.example.mdemo.model.Person"/> </typeAliases> <environments default="development"> <environment id="development"> <transactionManager type="JDBC"/> <dataSource type="POOLED"> <property name="driver" value="com.mysql.jdbc.Driver"/> <property name="url" value="jdbc:mysql://127.0.0.1:3306/demo01" /> <property name="username" value="root"/> <property name="password" value="root"/> </dataSource> </environment> </environments> <mappers> <mapper resource="com/example/mdemo/model/Person.xml"/> </mappers> </configuration>
3、定义模型和接口JAVA类,如下:
public class Person { private String FirstName; private String LastName; private int Age; public String getFirstName() { return FirstName; } public void setFirstName(String firstName) { FirstName = firstName; } public String getLastName() { return LastName; } public void setLastName(String lastName) { LastName = lastName; } public int getAge() { return Age; } public void setAge(int age) { Age = age; } }
public interface IPersonOperation { public Person selectPersonsByFirstName(String FirstName); public List<Person> selectPersons(String FirstName); public void addPerson(Person person); public void updatePerson(Person person); public void deletePerson(String FirstName); }
4、配置对应的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.example.mdemo.service.IPersonOperation"> <select id="selectPersonsByFirstName" parameterType="String" resultType="Person"> select * from `persons` where FirstName = #{FirstName} limit 1 </select> <select id="selectPersons" parameterType="string" resultMap="resultListPersons"> select * from persons where FirstName like #{FirstName} </select> <resultMap type="Person" id="resultListPersons"> <result column="FirstName" property="FirstName" /> <result column="LastName" property="LastName" /> <result column="Age" property="Age" /> </resultMap> <insert id="addPerson" parameterType="Person"> insert into persons(FirstName,LastName,Age) values(#{FirstName},#{LastName},#{Age}) </insert> <update id="updatePerson" parameterType="Person" > update persons set FirstName=#{FirstName},LastName=#{LastName},Age=#{Age} where FirstName=#{FirstName} </update> <delete id="deletePerson" parameterType="String"> delete from persons where FirstName=#{FirstName} </delete> </mapper>
5、测试使用如下:
public class Test { private static SqlSessionFactory sqlSessionFactory; private static Reader reader; static{ try{ reader = Resources.getResourceAsReader("config/Configuration.xml"); sqlSessionFactory = new SqlSessionFactoryBuilder().build(reader); }catch(Exception e){ e.printStackTrace(); } } public static SqlSessionFactory getSession(){ return sqlSessionFactory; } public static void main(String[] args) { SqlSession session = sqlSessionFactory.openSession(); /* try { Person user = (Person) session.selectOne("com.example.mdemo.model.PersonMapper.selectPersonsByFirstName", "FPPAU"); System.out.println(user.getFirstName()); System.out.println(user.getLastName()); System.out.println(user.getAge()); } finally { session.close(); }*/ try{ IPersonOperation personOperation = session.getMapper(IPersonOperation.class); Person person = personOperation.selectPersonsByFirstName("FPPAU"); System.out.println(person.getFirstName()); System.out.println(person.getLastName()); System.out.println(person.getAge()); List<Person> persons = personOperation.selectPersons("FPPAU"); for(Person p:persons){ System.out.println(p.getFirstName()); System.out.println(p.getLastName()); System.out.println(p.getAge()); } System.out.println(persons.size()); Person pp = new Person(); pp.setFirstName("Xu"); pp.setLastName("OK"); pp.setAge(40); personOperation.addPerson(pp); session.commit(); Person ppp = new Person(); ppp.setFirstName("Xu"); ppp.setLastName("OK"); ppp.setAge(20); personOperation.updatePerson(ppp); session.commit(); personOperation.deletePerson("FPPAU"); session.commit(); session.commit(); }finally{ session.close(); } } }
标签:
原文地址:http://www.cnblogs.com/Fredric-2013/p/5141360.html