首先下载Mybatis,加入jar包
编写Mybatis的.xml配置文件
然后编写“XXX“Mapper.xml的映射文件
之后用java调用就行了
import java.io.IOException; import java.io.InputStream; import org.apache.ibatis.io.Resources; import org.apache.ibatis.session.SqlSession; import org.apache.ibatis.session.SqlSessionFactory; import org.apache.ibatis.session.SqlSessionFactoryBuilder; public class MybTest { public void MybTest() throws IOException{ String resource = "com/config/mybatis-config.xml"; InputStream inputStream = Resources.getResourceAsStream(resource); SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(inputStream); SqlSession sqlSession = sqlSessionFactory.openSession(); //操作数据库,第一个参数是映射的Mapper.xml设定的sql语句,第二个是动态生成sql语句的参数 sqlSession.insert("test.addUser", user);//增 sqlSession.delete("test.deleteUserById", uid);//删 sqlSession.update("test.updateUserById", user);//改 sqlSession.selectOne(arg0, arg1);//查单个 sqlSession.selectList(arg0, arg1);//查多个 sqlSession.commit(); sqlSession.close(); } }