mybatis使用实例

1.jdbc.properties

jdbc.driver=com.mysql.jdbc.Driver
jdbc.url=jdbc:mysql://localhost:3306/housework
jdbc.username=root
jdbc.password=

2.编写mybatis核心配置文件 SqlMapConfig.xml








    

    
        
        
    

    
        
            
            
                
                
                
                
            
        
    

    
        
        
    

3.映射文件 UserMapper.xml 和mapper包中接口的方法对应








    

    

    
        insert into user set id=#{id},NAME=#{NAME},profession=#{profession}
    

    
        delete from user where id=#{id}
    

4.mapper包接口中的方法

public interface UserMapper {
    public List findAll();

    public List findUser(User user);

    public void findAdministrator(Administrator administrator);

    public void findAdd(User user);

    public void SelectUser(User user);
}

5.使用:

private InputStream resourceAsStream = Resources.getResourceAsStream("SqlMapConfig.xml");
private SqlSessionFactory sqlSessionFactory=new SqlSessionFactoryBuilder().build(resourceAsStream);
private SqlSession sqlSession = sqlSessionFactory.openSession();
private UserMapper mapper = sqlSession.getMapper(UserMapper.class);
//查询全部信息
List list=mapper.findAll();
//添加一条信息
mapper.findAdd(user);

你可能感兴趣的:(mysql,intellij-idea,java)