Brief introduction
I. Introduction Framework
- Use angle: Semi-finished piece of software, only need to fill in some of our changes.
- Benefits: The code is built on experienced people to write code, high code quality, good readability.
Two, java web framework related
- Data storage: hibermate / mybatis
- MVC :struts2 / spring mvc
- JavaSpript:jQuery
Third, tools
- spring produced STS, try using a lower version
MyBatis use
First, download the plug-jboss
- Click the help of the Eclipse Marketplace ... jboss input in the find, click go running, look for Red Hat CodeReady ..
Second, the initial use of mybatis (manual)
- New Java Project project, create a lib folder into which the package will mybatis jar.
- Right-click on the jar package select Build Path, click Add to Build Path.
- Because the need to connect to the database, the database should therefore jar package, the steps above.
Third, write a configuration file
- New Package in src, create named the mybatis-config.xml xml file. Mybatis be modified copied from the official documents in the configuration file, the code is as follows:
<?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="The POOLED " >
<-! Tell the driver class ->
<Property name = " Driver " value = " com.mysql.jdbc.Driver " />
<-! Connect to the database of the URL of ->
<Property name = " url " value = " jdbc: MySQL: // localhost: 3306 / Xcxy " />
! <- user name ->
<Property name = " username " value = " root " />
! <- password ->
<property name="password" value="123456" />
</dataSource>
</environment>
</environments>
<!--resource后写Mapper的地址 -->
<mappers>
<mapper resource="cn/edu/xcu/mybatisdemo/UserMapper.xml" />
</mappers>
</configuration>
Fourth, write Mapper maps that write sql statement place
- Creating UserMapper.xml file, copied from the official document, the code is as follows:
<?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="cn.edu.xcu.mybatisdemo.UserMapper"> -- namespace命名空间,解决重名问题 <select id="selectUser" parameterType="int" -- parameterType参数类型,resultType返回值类型 resultType="cn.edu.xcu.mybatisdemo.User"> select *from t_user where id = #{id} --#{}是为了区分列名与传的参数 </select> </mapper>
五、测试运行
public class MyBatisFind { public static void main(String[] args) throws IOException { //读取文件,连接到数据库 String resource = "mybatis-config.xml"; Reader reader = Resources.getResourceAsReader(resource); SqlSessionFactory sqlsessionfactory= new SqlSessionFactoryBuilder().build(reader); SqlSession sqlsession=sqlsessionfactory.openSession(); //查询user User user=sqlsession.selectOne("cn.edu.xcu.mybatisdemo.UserMapper.selectUser",1); System.out.println(user); sqlsession.close(); } }