UserMapper.xml
<?xml version="1.0" encoding="UTF-8" ?>
sqlMapConfig.xml
<?xml version="1.0" encoding="UTF-8" ?>
接口
package com.dao;import com.domain.User;public interface UserMapper { public void save(User user);}
User类不变
改变MybatisTest文件
package com.test;import com.dao.UserMapper;import com.domain.User;import org.apache.ibatis.io.Resources;import org.apache.ibatis.session.SqlSession;import org.apache.ibatis.session.SqlSessionFactory;import org.apache.ibatis.session.SqlSessionFactoryBuilder;import org.junit.Before;import org.junit.Test;import java.io.IOException;import java.io.InputStream;public class MybatisTest { private UserMapper mapper; @Before public void before() throws IOException { InputStream resourceAsStream = Resources.getResourceAsStream("sqlMapConfig.xml"); SqlSessionFactory sessionFactory = new SqlSessionFactoryBuilder().build(resourceAsStream); SqlSession sqlSession = sessionFactory.openSession(true); mapper = sqlSession.getMapper(UserMapper.class); } @Test public void testSave(){ User user=new User(); user.setUsername("mike"); user.setPassword("123"); mapper.save(user); }}
其余增删查改方法类似,看之前博客,然后再test方法里修改
3、注解开发完成基本的crud删去包含UerMapper.xml的包
package com.dao;import com.domain.User;import org.apache.ibatis.annotations.Delete;import org.apache.ibatis.annotations.Insert;import org.apache.ibatis.annotations.Select;import org.apache.ibatis.annotations.Update;public interface UserMapper { @Insert("insert into user values(#{id},#{username},#{password},#{birthday})") public void save(User user); //@Update("update user set username=#{username},password=#{password} where id=#{id}") //@Delete("delete from user where id=#{id}") //@Select("select * from user"}
<?xml version="1.0" encoding="UTF-8" ?>
直接运行Test即可
4、一对一的注解开发
黑马程序员ssm框架视频P189-P190
P191
6、多对多查询P192