发表于: 2018-03-20 23:46:17

1 544


一。今天把包含数据库的代码部署到服务器上:

首先确保在本地运行没有问题

在本地运行没问题之后,通过navicat连接阿里云的mysql数据库,建立一个新的数据库,数据表,和本地重名。

表里面的数据相同。

打包的时候,出现了问题,打完包大小才3.2K。放在阿里云上运行也不成功。

然后找了个插件,加入IDEA的pom.xml文件里

打包不成功有可能缺少一个插件
把以下代码放入POM.xml文件就可以了
 <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-shade-plugin</artifactId>
                <version>2.4.1</version>
                <executions>
                    <execution>
                        <phase>package</phase>
                        <goals>
                            <goal>shade</goal>
                        </goals>
                        <configuration>
                            <transformers>
                                <transformer implementation="org.apache.maven.plugins.shade.resource.ManifestResourceTransformer">
                                    <mainClass>cn.summerwaves.main.MainApp</mainClass>
                                </transformer>
                            </transformers>
                        </configuration>
                    </execution>
                </executions>
            </plugin>

红色字体改成类名,右键copy reference

继续clean package就可以啦。现在本地的命令行试一下

图,妥妥的。

然后上传到阿里云,运行,成功了。输入java -jar JdbcTest-1.0-SNAPSHOT-shaded.jar

(顺便还练习了几个linux命令)

二。重新在网上找了个教程,试了一下mybatis的多表查询,首先在命令行里试了一下

然后用java做,IDEA架构图为:

package com.dong.model;

public class Order {
private String id;
   private String number;
   private int price;
   private Person person;
 public String getId() {
return id;
   }
public void setId(String id) {
this.id = id;
   }
public String getNumber() {
return number;
   }
public void setNumber(String number) {
this.number = number;
   }
public int getPrice() {
return price;
   }
public void setPrice(int price) {
this.price = price;
   }
public Person getPerson() {
return person;
   }
public void setPerson(Person person) {
this.person = person;
   }
@Override
   public String toString() {
return "{id: " + id + ",number: " + number + ",price: " + price + "}";
   }
}


package com.dong.model;
public class Person {
private String id;
   private String name;
   private String address;
   private String tel;
   private List<Order> orders;
  public String getId() {
return id;
   }
public void setId(String id) {
this.id = id;
   }
public String getName() {
return name;
   }
public void setName(String name) {
this.name = name;
   }
public String getAddress() {
return address;
   }
public void setAddress(String address) {
this.address = address;
   }
public String getTel() {
return tel;
   }
public void setTel(String tel) {
this.tel = tel;
   }
public List<Order> getOrders() {
return orders;
   }
public void setOrders(List<Order> orders) {
this.orders = orders;
   }
@Override
   public String toString() {
return "{id: " + id + ",name: " + name + ",address: " + address + ",tel: " + tel + "}";
   }
}


<?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>
   <typeAliases>
       <typeAlias type="com.dong.model.Person" alias="Person"/>
       <typeAlias type="com.dong.model.Order" alias="Order"/>
   </typeAliases>
   <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/test" />
               <property name="username" value="root" />
               <property name="password" value="1234" />
           </dataSource>
       </environment>
   </environments>
   <mappers>
       <!-- 映射文件的位置 -->
       <mapper resource="Person.xml" />
       <mapper resource="Order.xml" />
   </mappers>
</configuration>
<?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="com.dong.model.Order">
   <resultMap type="Order" id="orderBean">
       <id column="orderId" property="id"/>
       <result column="orderNumber" property="number"/>
       <result column="orderPrice" property="price"/>

       <!-- 多对一的关系 -->
       <!-- property: 指的是属性的值, javaType:指的是属性的类型-->
       <association property="person" javaType="Person">
           <id column="personId" property="id"/>
           <result column="personName" property="name"/>
           <result column="personAddress" property="address"/>
           <result column="personTel" property="tel"/>
       </association>
   </resultMap>

   <!-- 根据id查询Order, 关联将Person查询出来 -->
   <select id="selectOrderById" parameterType="string" resultMap="orderBean">
       select p.*, o.* from person p, orders o where p.personId = o.pid and o.orderId = #{id}
</select>

</mapper>
<?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="com.dong.model.Person">

   <resultMap type="Person" id="personBean">
       <id column="personId" property="id"/>
       <result column="personName" property="name"/>
       <result column="personAddress" property="address"/>
       <result column="personTel" property="tel"/>

       <!-- 一对多的关系 -->
       <!-- property: 指的是集合属性的值, ofType:指的是集合中元素的类型 -->
       <collection property="orders" ofType="Order">
           <id column="orderId" property="id"/>
           <result column="orderNumber" property="number"/>
           <result column="orderPrice" property="price"/>
       </collection>
   </resultMap>

   <!-- 根据id查询Person, 关联将Orders查询出来 -->
   <select id="selectPersonById" parameterType="string" resultMap="personBean">
       select p.*, o.* from person p, orders o where p.personId = o.pid and p.personId = #{id}
</select>

</mapper>
public class POTest {
private SqlSessionFactory ssf;

   @Before
   public void initSF() throws Exception {
String resource = "mybatis-config.xml";
       InputStream inputStream = Resources.getResourceAsStream(resource);
       ssf = new SqlSessionFactoryBuilder().build(inputStream);
   }

@Test
   //一对多关联查询
   public void selectPersonById()throws Exception{
SqlSession session = ssf.openSession();
       Person person = session.selectOne("com.dong.model.Person.selectPersonById", "001");
       System.out.println(person.getOrders());
   }

@Test
   //多对一关联查询
   public void selectOrderById()throws Exception{
SqlSession session = ssf.openSession();
       Order order = session.selectOne("com.dong.model.Order.selectOrderById", "O_00001");
       System.out.println(order.getPerson().getName());
   }
}

运行,成功:


[{id: o_00001,number: 00001,price: 100}, {id: o_00002,number: 00002,price: 200}]

Jack

Process finished with exit code 0


明天的计划:任务一这样就复习完了,明天复习任务二

遇到的问题:暂无

今天的收获:在服务器部署了代码

java任务二开始时间:2018.01.25

预计demo时间:2018.02.12

可能有延期风险,原因是:json看不懂,控制器的逻辑看不懂,所以又回看了java语法

禅道链接地址:http://task.ptteng.com/zentao/project-task-501.html 



返回列表 返回列表
评论

    分享到