1最原始的mybatis使用方式
核心配置文件sqlMapConfig.xml文件
还要注意每个sqlMapConfig.xml文件中的标签的顺序,比如下面的就必须properties放在typeAliases前面
properties>settings>typeAliases>typeHandlers>objectFactory>objectWrapperFactory>plugins>environments>databaseIdProvider>mappers
<configuration> <properties resource="jdbc.properties"/> //使用properties文件避免硬编码问题 <typeAliases> <package name="com.wang.mybatis.pojo"/> //设置别名,对应pojo的mapper类就不用指定类的全路径名,直接写类的名字就可以了,而且不区分大小写 </typeAliases> <!-- 和spring整合后 environments配置将废除 --> <environments default="development"> <environment id="development"> <!-- 使用jdbc事务管理 --> <transactionManager type="JDBC" /> <!-- 数据库连接池 --> <dataSource type="POOLED"> <property name="driver" value="${jdbc.driver}" /> <property name="url" value="${url}" /> <property name="username" value="${username}" /> <property name="password" value="${password}" /> </dataSource> </environment> </environments> <mappers> <mapper resource="sqlmap/User.xml"/> </mappers> </configuration>
usermapper.xml文件每个小的mapper.xml文件配置对应的一种pojo
在里面编写对应的pojo操作
<mapper namespace="user"> <select id="finduerbyid" parameterType="Integer" resultType="User"> select * from user where ID=#{v} </select> <select id="finduserbyname" parameterType="String" resultType="com.wang.mybatis.pojo.User"> select * from user where name=#{v} </select> <select id="finduser" parameterType="String" resultType="com.wang.mybatis.pojo.User"> SELECT * FROM user WHERE name LIKE "%"#{vaue}"%" </select> <!-- 插入数据并且返回其id --> <insert id="insertuser" parameterType="com.wang.mybatis.pojo.User"> insert into user(name,age,sex,birthday) values(#{name},#{age},#{sex},#{birthday}) <selectKey keyProperty="ID" resultType="Integer" order="AFTER"> select LAST_INSERT_ID() </selectKey> </insert> <update id="updateuser" parameterType="com.wang.mybatis.pojo.User"> update user set name=#{name},sex=#{sex},age=#{age},birthday=#{birthday} where id=7{#ID} </update> <delete id="deleteuser" parameterType="Integer"> delete from user where ID=#{v} </delete> </mapper>
本例中的pojo示例,需要实现get和set方法,这样才能实现对数据库的操作
public class User implements Serializable{ //注意实现序列化接口 String name; int age; String sex; Date birthday; int ID; public int getID() { return ID; } public void setID(int iD) { 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; } public String getSex() { return sex; } public void setSex(String sex) { this.sex = sex; } public Date getBirthday() { return birthday; } public void setBirthday(Date birthday) { this.birthday = birthday; } @Override public String toString() { return "User [name=" + name + ", age=" + age + ", sex=" + sex + ", birthday=" + birthday + "]"; } }
最后Junit测试
@Test public void fun() throws IOException { //加载核心配置文件 String resource="SqlMapConfig.xml" ; InputStream in=Resources.getResourceAsStream(resource); //获得工厂 SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(in); //打开会话 SqlSession sqlSession = sqlSessionFactory.openSession(); //执行mapper(写好的sql语句) User user = sqlSession.selectOne("user.finduerbyid", 1); //找对应的mapper.xml文件,即user(namespace).finduserbyid(对应的sql语句)
System.out.println(user); User user2 = sqlSession.selectOne("user.finduserbyname", "小王"); System.out.println(user2); List<User> userlist = sqlSession.selectList("user.finduser", "小"); for (User user3 : userlist) { System.out.println(user3); }
上面的test只是简单的测试,如果在dao开发中需要在每个dao实现类中声明sqlsessionfactory等一系列步骤,这样dao开发就会变得麻烦。所以下面的动态代理开发简化上面的步骤
mapper动态代理开发
四大原则:
1接口名称和mapper.xml文件中的namespace一致
2接口中方法名称和mapper文件中定义的操作语句的id一样
3接口中方法名称和mapper文件中定义的操作语句入参一致
4接口中方法名称和mapper文件中定义的操作语句返回值一致
sqlMapConfig.xml文件
<configuration> <environments default="development"> <environment id="development"> <!-- 使用jdbc事务管理 --> <transactionManager type="JDBC" /> <!-- 数据库连接池 --> <dataSource type="POOLED"> <property name="driver" value="com.mysql.jdbc.Driver" /> <property name="url" value="jdbc:mysql://localhost:3306/hello?characterEncoding=utf-8" /> <property name="username" value="root" /> <property name="password" value="" /> </dataSource> </environment> </environments> <mappers> <!-- <mapper resource="SqlMap/User.xml"/> --> <package name="com.wang.pojo"/> <!-- 如果含有多个mapper文件就指定扫描包 --> </mappers>
当含有多个mapper.xml文件时候(尽量放在一起)可以指定扫描包的形式
对应接口和
public interface UserMapper { public User FindUserByID(Integer i); public Integer count(); public List<User> SelectAll(); }
mapper.xml文件
<mapper namespace="com.wang.pojo.UserMapper"> <select id="FindUserByID" parameterType="Integer" resultType="com.wang.pojo.User"> SELECT *FROM USER WHERE ID=#{v}; </select> <select id="count" resultType="Integer"> SELECT COUNT(1) FROM USER </select> <select id="SelectAll" resultType="com.wang.pojo.User"> SELECT * FROM USER </select> </mapper>
pojo和上面的一样,这里就不在写上
最后测试
@Test public void testName() throws Exception { String Resource ="SqlMapConfig.xml"; //弄成输入流 InputStream in = Resources.getResourceAsStream(Resource); //用builder创建sqlsession工厂 SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(in); //打开会话 SqlSession sqlSession = sqlSessionFactory.openSession(); //获得接口 UserMapper userMapper = sqlSession.getMapper(UserMapper.class); //执行接口中的方法 User user=userMapper.FindUserByID(3); System.out.println(user); System.out.println(userMapper.count()); List<User> users=userMapper.SelectAll(); for (User user2 : users) { System.out.println(user2); } }
前者使用sqlSession直接操作,后者用sqlSession获得接口,使用接口操作