Mybatis-使用getMapper()面向借口编程

步骤如下:

1、实体类:

public class Userinfo {

private Long id;

private String username;

private String password;

private Long age;     

//get和set方法

}

2、接口:

public interface UserinfoMapper {

	public void insertUserinfo(Userinfo userinfo);
	
	public List<Userinfo> getAllUserinfo();
	
	public Userinfo getUserinfo(long userid);
	
	public void deleteUserinfo(long userid);
	
	public void updateUserinfo(Userinfo userinfo);
	
}
3、SQL映射文件:
<mapper namespace="com.wuly.num1.inter.UserinfoMapper">
  <insert id="insertUserinfo" parameterType="com.wuly.num1.vo.Userinfo" >

        <selectKey order="BEFORE" resultType="java.lang.Long"keyProperty="id">
		select idauto.nextval from dual
	</selectKey>

  	insert into userinfo(id,username,password,age)
  	values(#{id},#{username},#{password},#{age})
  </insert>
  <select id="getUserinfo" parameterType="long" resultType="com.wuly.num1.vo.Userinfo">
  	select * from 
  	userinfo where id=#{id}
  </select>
  <delete id="deleteUserinfo" parameterType="long">
  	delete from userinfo where id=#{id}
  </delete>
  <update id="updateUserinfo" parameterType="com.wuly.num1.vo.Userinfo">
  	update userinfo set username=#{username},password=#{password},age=#{age}
  	where id=#{id}
  </update>
  <select id="getAllUserinfo" resultType="com.wuly.num1.vo.Userinfo">
  	select * from userinfo
  </select>
</mapper>


4、mybatis-config.xml配置文件(在src目录下):

<configuration>
	<environments default="development">
		<environment id="development">
			<transactionManager type="JDBC"></transactionManager>
			<dataSource type="POOLED">
				<property name="driver" value="com.mysql.jdbc.Driver"/>
				<property name="url" value="jdbc:mysql://localhost:3306/mybatis"/>
				<property name="username" value="root"/>
				<property name="password" value="123456"/>
			</dataSource>
		</environment>
	</environments>
	<mappers>
		<mapper resource="userinfoMapping.xml"/>
	</mappers>
</configuration>

5、测试类:

public static void main(String[] args) {
		
		Userinfo userinfo = new Userinfo();
		userinfo.setUsername("hhh");
		userinfo.setPassword("1456");
		userinfo.setAge(25L);
		SqlSession sqlSession = TestOrm.getSqlSession();
		UserinfoMapper userinfoMapper = sqlSession.getMapper(UserinfoMapper.class);
		userinfoMapper.insertUserinfo(userinfo);
		/*List<Userinfo> list = userinfoMapper.getAllUserinfo();
		for(int i=0;i<list.size();i++){
			Userinfo user = list.get(i);
			System.out.println(user.getId()+">>"+user.getUsername()+">>"+user.getPassword()+">>"+user.getAge());
		}*/
		/*Userinfo user = userinfoMapper.getUserinfo(1);
		System.out.println(user);
		
		user.setPassword("8888");
		userinfoMapper.updateUserinfo(user);*/
		userinfoMapper.deleteUserinfo(2);
		sqlSession.commit();
		sqlSession.close();
		
		System.out.println("createId=" + userinfo.getId());
	}
	
	public static SqlSession getSqlSession(){
		String resource = "mybatis-comfig.xml";
		SqlSession sqlSession = null;
		try {
			InputStream inputStream = Resources.getResourceAsStream(resource);
			SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(inputStream);
			sqlSession = sqlSessionFactory.openSession();
		} catch (IOException e) {
			e.printStackTrace();
		}
		return sqlSession;		
	}
}

如果按这样的写完,运行时发现控制台报错:Unknown table 'idauto' in field list

原因是映射文件里面的写法有问题,本来是想将id设置为主键,自动增长的,但是用语句写在MySQL里面不行,最后我解决的办法是:将映射文件开始的语句换成:

<insert id="insertUserinfo" parameterType="com.wuly.num1.vo.Userinfo" 
    useGeneratedKeys="true" keyProperty="id">
  insert into userinfo(id,username,password,age)
  values(#{id},#{username},#{password},#{age})

  </insert>

并且在建表时,设置id为主键并自增。


阅读更多
文章标签: JavaEE
个人分类: MyBatis
想对作者说点什么? 我来说一句

没有更多推荐了,返回首页

关闭
关闭
关闭