MyBatis总结(一)
一、创建测试项目工程
二、导包(一个myBatis所需的包,以及一个数据库操作的包)
三、创建实体类
四、配置文件的建立(最佳命名为(SqlMapConfig.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> <properties resource="datasource.properties"></properties> <typeAliases> <typeAlias type="com.lovo.pojo.User" alias="User"/> </typeAliases> <environments default="development"> <environment id="development"> <transactionManager type="JDBC" /> <dataSource type="POOLED"> <property name="driver" value="${jdbc.driver}" /> <property name="url" value="${jdbc.url}" /> <property name="username" value="${jdbc.username}" /> <property name="password" value="${jdbc.password}" /> </dataSource> </environment> </environments> <mappers> <mapper resource="com/lovo/mapper/UserMapper.xml"/> </mappers> </configuration>
用以上方式配置需要先创立配置文件(datasource.properties)
配置文件代码如下:
jdbc.driver=com.mysql.jdbc.Driver jdbc.url=jdbc:mysql://localhost:3306/myBatis?useUnicode=true&characterEncoding=UTF-8 jdbc.username=root jdbc.password=123
然后创建一个获得连接对象的类,我习惯命名为DbUtil放在util包中,代码如下:
public class DbUtil { private static SqlSessionFactory sessionFactory = null; private DbUtil() { InputStream in = null; try { in = Resources.getResourceAsStream("SqlMapConfig.xml"); sessionFactory = new SqlSessionFactoryBuilder().build(in); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } } public static SqlSession getSession() { if (sessionFactory == null) { new DbUtil(); } return sessionFactory.openSession(); }
然后再对实体pojo配置一个XML文件和一个接口:
XML最好与接口同名,以便自己好辨识
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" > <!--namespace与mapper接口相对应 --> <mapper namespace="com.lovo.mapper.UserMapper"> <resultMap type="User" id="UserMap"> <id property="id" column="id" javaType="int"/> <result property="userName" column="username" javaType="java.lang.String"/> <result property="userPwd" column="pwd" javaType="java.lang.String"/> <result property="state" column="state" javaType="int"/> </resultMap> <insert id="addUser" parameterType="User"> insert into user values(null,#{User.userName},#{User.userPwd},#{User.state}) </insert> <delete id="delUserById" parameterType="int"> delete from user where id=#{id} </delete> <select id="findUserById" parameterType="int" resultMap="UserMap"> select * from user where id=#{id} </select> <update id="updateUser" parameterType="User"> update user set username=#{User.userName},pwd=#{User.userPwd},state=#{User.state} where id=#{User.id} </update> <select id="FindAllUser" resultMap="UserMap"> select * from user </select> <select id="findUserCount" resultType="int"> select count(*) from user </select> <select id="FindUserByState" resultMap="UserMap"> select * from user where state=#{state} </select> <select id="sumOfState" resultType="int"> select sum(state) from user </select> </mapper>
接口如下:
public interface UserMapper { public int addUser(@Param("User") User user); public int delUserById(@Param("id") int id); public int updateUser(@Param("User") User user); public User findUserById(@Param("id") int id); public List<User> FindAllUser(); public int findUserCount(); public List<User> FindUserByState(@Param("state") int state); public int sumOfState(); }
这里的@param("User")为别名,除参数只有单个,且为基本数据类型外,都需要给于别名。
最后在建立service层
如:
public class UserServiceImpl implements UserService { SqlSession sqlSession=DbUtil.getSession(); UserMapper mapper=sqlSession.getMapper(UserMapper.class); @Override public int addUser(User user) { // TODO Auto-generated method stub int num=0; num=mapper.addUser(user); sqlSession.commit(); return num; }