MyBatis半orm
开发效率比hibernate低,比jdbc高。
mybatis-config.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 type="com.sw.entity.Users" alias="a"></typeAlias>-->
<package name="com.sw.entity"/>
</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/t202?characterEncoding=utf-8"/>
<property name="username" value="root"/>
<property name="password" value="123456"/>
</dataSource>
</environment>
</environments>
<!--添加映射文件 映射实体类的映射文件-->
<mappers>
<mapper resource="com/sw/entity/Users.xml"></mapper>
</mappers>
</configuration>
实体类映射文件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.sw.dao.UsersDao"><!--namespace就是定义的接口包名+类名-->
<!--查询users表中的所有数据
id:该映射被调用的时候的调用名称
parameterType="" 参数类型 无参数不写
-->
<select id="findAllUsers" resultType="Users">
select * from users;
</select>
<!--通过编号查询用户信息-->
<select id="findUsersById" parameterType="String" resultType="Users">
select * from users where uname=#{uname}
</select>
<!--添加数据-->
<insert id="addUsers" parameterType="Users">
insert into users values(null,#{uname},#{sex},#{birthday},#{address});
</insert>
<update id="updateUsers" parameterType="Users">
update users set uname=#{uname},sex=#{sex} where uid=#{uid};
</update>
<!--删除的映射-->
<delete id="deleteUsers" parameterType="int">
delete from users where uid=#{uid};
</delete>
</mapper>