开发Web应用,数据的存储和处理往往离不开数据库和SQL语句。在使用Java开发的Web应用中,自然也少不了连接数据库的步骤。在底层连接数据库的时候,一般优胜JDBC技术,也就是Java的一种提供数据库连接和操作SQL的底层API。但是互联网技术正在飞速发展,使用原始JDBC已经满足不了项目的开发需求了,这就使得Hibernate,MyBatis(iBatis),JPA,JDO等一些优秀的ORM框架诞生,它们不公结合了原生JDBC的功能,还使开发简捷化,规范化。
前一章我讲了使用maven在idea下搭建SpringMVC,下面接着讲使用maven在idea下搭建MyBatis。
首先在mysql中创建一个名为test的数据库,然后在数据库中创建表“userinfo”:
1,打开之前项目,pom.xm中加入MyBatis的引用:
<dependency> <groupId>log4j</groupId> <artifactId>log4j</artifactId> <version>1.2.12</version> </dependency> <dependency> <groupId>org.slf4j</groupId> <artifactId>slf4j-api</artifactId> <version>1.7.20</version> </dependency> <dependency> <groupId>org.mybatis</groupId> <artifactId>mybatis</artifactId> <version>3.4.0</version> </dependency> <dependency> <groupId>mysql</groupId> <artifactId>mysql-connector-java</artifactId> <version>5.1.41</version> </dependency> <dependency> <groupId>com.microsoft.sqlserver</groupId> <artifactId>sqljdbc4</artifactId> <version>4.0</version> </dependency>
2,在Java文件夹下创建包"cn.com.mybatis.pojo",在包下创建java类"UserInfo":
package cn.com.mybatis.pojo; import java.io.Serializable; public class UserInfo implements Serializable { private int id; public int getId() { return id; } public void setId(int id) { this.id = id; } public String getUserName() { return userName; } public void setUserName(String userName) { this.userName = userName; } private String userName; public String getPassword() { return password; } public void setPassword(String password) { this.password = password; } private String password; private int age; public int getAge() { return age; } public void setAge(int age) { this.age = age; } }
2,在"resource"文件夹下新建一个"mybatis"文件夹,
编写SQL映射文件:
在mybatis文件夹加新建一个mapper文件夹,并在mapper下创建名为userinfo的xml文件,编辑如下:
<?xml version="1.0" encoding="UTF-8"?> <!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd"> <mapper namespace="test"> <select id="findById" parameterType="int" resultType="cn.com.mybatis.pojo.UserInfo"> SELECT * FROM userinfo WHERE id=#{id} </select> </mapper>
在"mybatis"文件夹下新建名为"SqlMapConfig"的xml文件,并编辑:
<?xml version="1.0" encoding="UTF-8" ?> <!DOCTYPE configuration PUBLIC "-//mybatis.org//DTDConfig 3.0//EN" "http://mybatis.org/dtd/mybatis-3-config.dtd"> <configuration> <environments default="development"> <environment id="development"> <transactionManager type="JDBC"></transactionManager> <dataSource type="POOLED"> <property name="drever" value="org.gjt.mm.mysql.Driver"></property> <property name="url" value="jdbc:mysql://localhost:3306/test?characterEncoding=utf-8"></property> <property name="username" value="root"></property> <property name="password" value="123456"></property> </dataSource> </environment> </environments> <mappers> <mapper resource="mybatis/mapper/userinfo.xml"></mapper> </mappers> </configuration>
3,在Java文件夹下创建包"cn.com.mybatis.datasource",在包下创建并编写数据库交互类(DataConnection):
package cn.com.mybatis.datasource; import org.apache.ibatis.io.Resources; import org.apache.ibatis.session.SqlSession; import org.apache.ibatis.session.SqlSessionFactory; import org.apache.ibatis.session.SqlSessionFactoryBuilder; import java.io.IOException; import java.io.InputStream; public class DataConnection { private String resource="mybatis/SqlMapConfig.xml"; private SqlSessionFactory sqlSessionFactory; private SqlSession sqlSession; public SqlSession getSqlSession() throws IOException{ InputStream inputStream= Resources.getResourceAsStream(resource); sqlSessionFactory=new SqlSessionFactoryBuilder().build(inputStream); sqlSession=sqlSessionFactory.openSession(); return sqlSession; } }
在Java文件夹下创建包"cn.com.mybatis.dao",在包下创建并编写数据库访问类(UserInfoDAO):
package cn.com.mybatis.dao; import cn.com.mybatis.datasource.DataConnection; import cn.com.mybatis.pojo.UserInfo; import org.apache.ibatis.session.SqlSession; import java.io.IOException; public class UserInfoDAO { public DataConnection dataConn = new DataConnection(); public UserInfo GetUserById() throws IOException{ SqlSession sqlSession=dataConn.getSqlSession(); UserInfo user = sqlSession.selectOne("test.findById",1); sqlSession.close(); return user; } }
最后,在表现层显示数据:
修改src/main/webapp/Java/cn.com.controller/Home
package cn.com.controller; import cn.com.mybatis.dao.UserInfoDAO; import cn.com.mybatis.pojo.UserInfo; import org.springframework.stereotype.Controller; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.servlet.ModelAndView; import java.io.IOException; @Controller @RequestMapping("/Home") public class Home { @RequestMapping("/Index") public ModelAndView Index() throws IOException { ModelAndView view = new ModelAndView(); UserInfoDAO userInfoDTO=new UserInfoDAO(); UserInfo user = userInfoDTO.GetUserById(); view.addObject("userInfo",user); view.setViewName("Index"); return view; } }
修改Index.JSP文件
<%-- Created by IntelliJ IDEA. User: Administrator Date: 2018-12-07 Time: 16:52 To change this template use File | Settings | File Templates. --%> <%@ page contentType="text/html;charset=UTF-8" language="java" isELIgnored="false" %> <html> <head> <title>Title</title> </head> <body> <h2>Index</h2> <ul> <li>${userInfo.id}</li> <li>${userInfo.userName}</li> <li>${userInfo.age}</li> </ul> </body> </html>
大功告成!
运行Tomcat
目录结构:
资源下载地址:https://download.csdn.net/download/wkjiteye/10838188