mybatis
String resource = "mybatis-config.xml"; InputStream inputStream = getResourceAsStream(resource); SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(inputStream); SqlSession sqlSession = sqlSessionFactory.openSession(); Map<String,String> map = new HashMap<String,String>(); map.put("id", "029c987e4a9745b38aa65e46d8c77a01"); User user = sqlSession.selectOne("com.mytest.UserMapper.UserMapper.selectUser", map); User userinsert = new User("11101", "222", "333"); int a = sqlSession.insert("com.mytest.UserMapper.UserMapper.insertUser", userinsert); sqlSession.commit(); sqlSession.close();
把mybatis 的配置文件放到 src/main/resources 下,文件内容如下:
<?xml version="1.0" encoding="UTF-8" ?> <!DOCTYPE configuration PUBLIC "-//mybatis.org//DTD Config 3.0//EN" "http://mybatis.org/dtd/mybatis-3-config.dtd"> <configuration> <environments default="development"> <environment id="development"> <transactionManager type="JDBC"/> <dataSource type="POOLED"> <property name="driver" value="com.mysql.jdbc.Driver"/> <property name="url" value="jdbc:mysql://192.168.1.193:3306/test?characterEncoding=utf8"/> <property name="username" value="root"/> <property name="password" value="111"/> </dataSource> </environment> </environments> <mappers> <mapper resource="com/mytest/UserMapper/UserMapper.xml"/> </mappers> </configuration>
test 是数据库名,