下面是我对 "Javaweb mybatis接口开发实现过程详解" 的完整攻略,以及包含两条示例。
<dependency>
<groupId>org.mybatis</groupId>
<artifactId>mybatis</artifactId>
<version>3.5.5</version>
</dependency>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>8.0.22</version>
</dependency>
示例:
<settings>
<setting name="mapUnderscoreToCamelCase" value="true"/>
</settings>
<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://localhost:3306/mybatis"/>
<property name="username" value="root"/>
<property name="password" value="root"/>
</dataSource>
</environment>
</environments>
示例:
<mapper namespace="org.mybatis.example.BlogMapper">
<select id="selectBlog" resultType="Blog">
select * from Blog where id = #{id}
</select>
</mapper>
示例:
Configuration config = new Configuration();
config.set.... // 配置文件设置
SqlSessionFactory factory = new SqlSessionFactoryBuilder().build(config);
SqlSession sqlSession = factory.openSession();
User user = sqlSession.selectOne("org.mybatis.spring.sample.mapper.UserMapper.getUser", "USER001");
sqlSession.close();
示例:
public class User {
private int id;
private String name;
private int age;
// getter和setter方法
}
示例:
public interface UserMapper {
@Insert("INSERT INTO user(name, age) VALUES(#{name}, #{age})")
void addUser(User user);
@Select("SELECT * FROM user WHERE id = #{id}")
User getUserById(int id);
}
示例:
<mappers>
<mapper class="com.example.dao.UserMapper"/>
</mappers>
示例:
<mapper namespace="com.example.dao.UserMapper">
<insert id="addUser" parameterType="com.example.bean.User">
INSERT INTO user(name, age) VALUES(#{name}, #{age})
</insert>
<select id="getUserById" parameterType="int" resultType="com.example.bean.User">
SELECT * FROM user WHERE id = #{id}
</select>
</mapper>
示例:
SqlSession session = sqlSessionFactory.openSession();
UserMapper userMapper = session.getMapper(UserMapper.class);
User user = userMapper.getUserById(1);
session.commit();
session.close();
OK,以上就是实现Javaweb mybatis接口开发的完整攻略,希望对你有所帮助。
本文链接:http://task.lmcjl.com/news/7944.html