3.需要的jar包
4.配置文件:放在config目录下# Global logging configuration
log4j.rootLogger=DEBUG, stdout
# Console output...
log4j.appender.stdout=org.apache.log4j.ConsoleAppender
log4j.appender.stdout.layout=org.apache.log4j.PatternLayout
log4j.appender.stdout.layout.ConversionPattern=%5p [%t] - %m%n
select LAST_INSERT_ID()
insert into `user` (username ,birthday,sex ,address) VALUES (#{username},#{birthday},#{sex},#{address});
update user set address = #{address} where id = #{id}
delete from user where id = #{id}
public class MybatisTest {
SqlSessionFactory sessionFactory = null;
@Before
public void init() {
// TODO Auto-generated method stub
try {
SqlSessionFactoryBuilder sessionFactoryBuilder = new SqlSessionFactoryBuilder();
InputStream inputStream = Resources.getResourceAsStream("SqlMapConfig.xml");//加载配置文件
sessionFactory = sessionFactoryBuilder.build(inputStream);
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
@Test
public void testQueryUserById() throws Exception {
SqlSession session = sessionFactory.openSession();
User user = session.selectOne("queryUserById", 1); //selectOne返回一个查询结果
System.out.println(user);
session.close();
}
@Test
public void testQueryUserByName2() {
SqlSession session = sessionFactory.openSession();
List list = session.selectList("queryUserByName2", "王五"); //selectList是返回一个列表
for (User user : list) {
System.out.println(user);
}
session.close();
}
@Test
public void insetUser() {
// TODO Auto-generated method stub
SqlSession sqlSession = sessionFactory.openSession();
User user = new User("赵小虎",new Date(),'2',"蜀国");
sqlSession.insert("insetUser",user);
sqlSession.commit();
System.out.println(user);
sqlSession.close();
}
@Test
public void updateUserById() {
// TODO Auto-generated method stub
SqlSession sqlSession = sessionFactory.openSession();
User user = new User();
user.setId(26);
user.setAddress("刘家窑");
sqlSession.update("updateUserById", user);
sqlSession.commit();
sqlSession.close();
}
@Test
public void deleteUserById() {
// TODO Auto-generated method stub
SqlSession sqlSession = sessionFactory.openSession();
sqlSession.delete("deleteUserById",31);
sqlSession.commit();
sqlSession.close();
}
}
10.在src下创建mapper包,将userMapper.xml 和 (接口)UserMapper.java 放在一起。
UserMapper.xml
public interface UserMapper {
List queryUserByQueryVo(QueryVo queryVo);
}
public class userMapperTest {
private SqlSessionFactory sqlSessionFactory = null;
@Before
public void init() {
// TODO Auto-generated method stub
SqlSessionFactoryBuilder builder = new SqlSessionFactoryBuilder();
InputStream inputStream;
try {
inputStream = Resources.getResourceAsStream("SqlMapConfig.xml");
sqlSessionFactory = builder.build(inputStream);
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
@Test
public void queryUserByQueryVo() {
// TODO Auto-generated method stub
SqlSession sqlSession = sqlSessionFactory.openSession();
UserMapper mapper = sqlSession.getMapper(UserMapper.class);
QueryVo queryVo = new QueryVo();
User user = new User();
user.setUsername("刘");
queryVo.setUser(user);
List list = mapper.queryUserByQueryVo(queryVo);
for (User user2 : list) {
System.out.println(user2);
}
sqlSession.close();
}
}
id, username, birthday, sex, address