一.com/book/map包下有两个配置文件:
1.MyBatisConfig.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 alias="bookUser" type="com.book.pojo.bookUser" /> </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://localhost:3306/wzz"/> <property name="username" value="root"/> <property name="password" value="777777"/> </dataSource> </environment> </environments> <!-- 映射路径 --> <mappers> <mapper resource="com/book/map/bookmap.xml"/>
<!-- 接口的方式 --> <mapper class="com.book.map.InterfaceBookUserMap" /> </mappers> </configuration>
2.bookmap.xml 用于写sql语句进行数据库具体操作
<?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="domain.blog.mappers.AutoorMapper"> <!-- parameterType为参数类型,resultType为返回值类型 --> <select id="findById" parameterType="int" resultType="com.book.pojo.bookUser"> select * from bookuser where id=#{id} </select> <insert id="insertUser" parameterType="bookUser" statementType="PREPARED" keyProperty="id" useGeneratedKeys="true"> insert into bookuser (userName,password) values (#{userName},#{password}) </insert> <update id="updateUser" parameterType="bookUser"> update bookuser set userName=#{userName}, password=#{password} where id=#{id} </update> </mapper>
也可以用interface接口的方式取代
package com.book.map; import org.apache.ibatis.annotations.Delete; public interface InterfaceBookUserMap { @Delete("delete from bookuser where id=#{id}") public void deleteUser(Integer id); }
二.一个简单java类bookUser
package com.book.pojo; public class bookUser { private int id; private String userName; private String password; public int getId() { return id; } public void setId(int id) { this.id = id; } public String getUserName() { return userName; } public void setUserName(String userName) { this.userName = userName; } public String getPassword() { return password; } public void setPassword(String password) { this.password = password; } }
三.测试类
package com.book.test; import java.io.IOException; import java.io.Reader; import org.apache.ibatis.annotations.Update; import org.apache.ibatis.io.Resources; import org.apache.ibatis.session.SqlSession; import org.apache.ibatis.session.SqlSessionFactory; import org.apache.ibatis.session.SqlSessionFactoryBuilder; import com.book.map.InterfaceBookUserMap; import com.book.pojo.bookUser; public class testBook { public static void main(String[] args) { String resource = "com/book/map/MyBatisConfig.xml"; Reader reader = null; SqlSession session; try { reader = Resources.getResourceAsReader(resource); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } SqlSessionFactory sqlMapper = new SqlSessionFactoryBuilder().build(reader); session = sqlMapper.openSession(); //selectone(session); //insert(session); //update(session); delete(session); session.close(); } public static void selectone(SqlSession session){ bookUser temp = session.selectOne("findById",1); System.out.print(temp.getUserName()); } public static void insert(SqlSession session){ bookUser bu = new bookUser(); bu.setUserName("wzz"); bu.setPassword("123456"); session.insert("insertUser", bu); session.commit(); } public static void update(SqlSession session){ bookUser bu = new bookUser(); bu.setUserName("wzz2"); bu.setPassword("111"); bu.setId(1); session.update("updateUser", bu); session.commit(); } public static void delete(SqlSession session){ InterfaceBookUserMap idbu = session.getMapper(InterfaceBookUserMap.class); idbu.deleteUser(1); session.commit(); } }