什么是 MyBatis?
MyBatis 是支持普通 SQL 查询,软件开发,存储进程和高级映射的优秀耐久层框架。 MyBatis 消除了险些所有的 JDBC 代码和参数的手工配置以及对功效集的检索。MyBatis 可以利用简朴的XML 或注解用于设置和原始映射,将接口和 Java 的 POJO(Plain Old Java Objects,普通的Java工具)映射成数据库中的记录。
MyBatis下载:https://github.com/mybatis/mybatis-3/releases
Mybatis实例
对一个User表的CRUD操纵:
User表:
-- ---------------------------- -- Table structure for `user` -- ---------------------------- DROP TABLE IF EXISTS `user`; CREATE TABLE `user` ( `id` int(11) NOT NULL AUTO_INCREMENT, `userName` varchar(50) DEFAULT NULL, `userAge` int(11) DEFAULT NULL, `userAddress` varchar(200) DEFAULT NULL, PRIMARY KEY (`id`) ) ENGINE=InnoDB AUTO_INCREMENT=6 DEFAULT CHARSET=utf8; -- ---------------------------- -- Records of user -- ---------------------------- INSERT INTO `user` VALUES ('1', 'summer', '30', 'shanghai'); INSERT INTO `user` VALUES ('2', 'test2', '22', 'suzhou'); INSERT INTO `user` VALUES ('3', 'test1', '29', 'some place'); INSERT INTO `user` VALUES ('4', 'lu', '28', 'some place'); INSERT INTO `user` VALUES ('5', 'xiaoxun', '27', 'nanjing');
在Src目次下建一个mybatis的xml设置文件Configuration.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> <!-- mybatis别名界说 --> <typeAliases> <typeAlias alias="User" type="com.mybatis.test.User"/> </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/mybatis" /> <property name="username" value="root"/> <property name="password" value="admin"/> </dataSource> </environment> </environments> <!-- mybatis的mapper文件,每个xml设置文件对应一个接口 --> <mappers> <mapper resource="com/mybatis/test/User.xml"/> </mappers> </configuration>
界说User mappers的User.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.mybatis.test.IUserOperation"> <!-- select语句 --> <select id="selectUserByID" parameterType="int" resultType="User"> select * from `user` where user.id = #{id} </select> <!-- 界说的resultMap,可以办理类的属性名和数据库列名纷歧致的问题--> <!-- <resultMap type="User" id="userResultMap"> <id property="id" column="user_id" /> <result property="userName" column="user_userName" /> <result property="userAge" column="user_userAge" /> <result property="userAddress" column="user_userAddress" /> </resultMap> --> <!-- 返回list的select语句,劳务派遣管理系统,留意 resultMap的值是指向前面界说好的 --> <!-- <select id="selectUsersByName" parameterType="string" resultMap="userResultMap"> select * from user where user.userName = #{userName} </select> --> <select id="selectUsersByName" parameterType="string" resultType="User"> select * from user where user.userName = #{userName} </select> <!--执行增加操纵的SQL语句。id和parameterType别离与IUserOperation接口中的addUser要领的名字和参数范例一致。 useGeneratedKeys配置为"true"表白要MyBatis获取由数据库自动生成的主键;keyProperty="id"指定把获取到的主键值注入到User的id属性--> <insert id="addUser" parameterType="User" useGeneratedKeys="true" keyProperty="id"> insert into user(userName,userAge,userAddress) values(#{userName},#{userAge},#{userAddress}) </insert> <update id="updateUser" parameterType="User" > update user set userName=#{userName},userAge=#{userAge},userAddress=#{userAddress} where id=#{id} </update> <delete id="deleteUser" parameterType="int"> delete from user where id=#{id} </delete> </mapper>